Search code examples
ruby-on-railsformsput

how to set method put in form_tag in rails?


how to set method inside form to put in form_tag in rails?, i have form_tag like this :

<%= form_tag(url, :method => :put, :multipart => true, :class =>"form-horizontal") do %>
 ......
<% end %>

but if i inspect element, form not have method "put" but still "post"?

<form accept-charset="UTF-8" action="/admin/stores/1/information/social_update" class="form-horizontal" enctype="multipart/form-data" method="post">
 ....
</form>

why???


Solution

  • According to the docs:

    If “patch”, “put”, “delete”, or another verb is used, a hidden input with name _method is added to simulate the verb over post.

    Therefore, the following code will output the following markup:

    form_tag('/posts/1', method: :put)
    #=> <form action="/posts/1" method="post"> ... <input name="_method" type="hidden" value="put" /> ...
    

    This is basically a backwards compatible way of implementing the PUT protocol across browsers. Although the form's submission method is POST, because of the hidden form input, Rails understands the desired submission method to be PUT.