Aravind
Aravind

Reputation: 1401

Rails simple form put/patch request go as post requests for some objects alone

We have form like this:

<%= simple_form_for(@venue, url: url, method: method) do |f| %>
 <%= render  "shared/errors", object: venue%>
  <div class="row">
    <div class="col-md-12">
      <div class="form-group label-floating">
        <%= f.association :partner, input_html: { class: "form-control", value: partner.try(:id)  }, label_html: { class: 'control-label' } %>
      </div>
    </div>
  </div>
<div class="row">
  <div class="col-md-12">
    <div class="form-group label-floating">
      <%= f.input :name, input_html: { class: "form-control"  }, label_html: { class: 'control-label' } %>
    </div>
  </div>
</div>

<div class="row">
  <div class="col-md-12">
    <div class="form-group label-floating">
      <%= f.input :description, required: true,label: 'About', input_html: { class: "form-control", rows: 5 }, label_html: { class: 'control-label' }  %>
    </div>
  </div>
</div>

When I pass the method as :put I get a hidden field like this in the form

<form novalidate="novalidate" class="simple_form edit_venue" id="edit_venue_19" action="/admin/venues/19" accept-charset="UTF-8" method="post"><input name="utf8" type="hidden" value="&#x2713;" />
  <input type="hidden" name="_method" value="put" />

For some objects the code is being submitted to the update action as expect but for some it goes as a post request. Is this a bug in rails? Or is there something that I am missing?

Upvotes: 1

Views: 2540

Answers (1)

Timo Schilling
Timo Schilling

Reputation: 3073

Not all browsers supporting put or patch. Rails solves this problem with the _method field. Rails has a Rack Middleware which rewrites the HTTP POST to HTTP PUT when the form sends the _method=put. If you take a look on your Rails console you can the that you receive a put/update request.

Upvotes: 5

Related Questions