What URL should I use to PUT an update?

2019-06-06 14:03发布

I have a Rails app that I just picked up that has the following command in "rake routes":

PUT    /testclass/:id(.:format)                      testclass#update

I want to send a PUT update to this testclass with the id 18445 and change finished to false:

/18445&finished=false

for example.

My understanding is this should be able to be done by a HTTP request in the browser, for example, can it? Or do I need to use a Ruby command? Any guidance?

1条回答
Emotional °昔
2楼-- · 2019-06-06 14:30

You do not need to use a ruby command to access that route to make an update.

Basic HTML will look something like this (you can edit the action to be relevant to your route):

    <form action='testclass/18445' method="post">
      <input type="hidden" name="_method" value="put">
      <input name="authenticity_token" value="<%= form_authenticity_token %>" type="hidden">
      <input type="hidden" name="testclass[id]" value="18445" >
      <input type="hidden" name="testclass[finished]" value="false" >
      <input type="submit" value="update" >
    </form>

Notice it is a 'post' but there is a hidden input with the name '_method' and value 'put'. Hopefully this is what you're looking for. You can send your id through a hidden input, same with the false value.

查看更多
登录 后发表回答