Question

I have a form in a partial. When the page loads the first time, it works fine. However, if the partial re-renders, then you submit the form, it tries to POST instead. I tried adding this line to force the PUT:

<input type="hidden" name="_method" value="put" />

, but then I got a "WARNING: Can't verify CSRF token authenticity"

Doing :method => :put, and :html=>{:method=>:put} had no affect.

How can I fix this?

partial:

<div id="item_form<%= item.id %>">
    <%= render 'edit_item_row', item: item %>
</div>

Form:

    <%= form_for item do |f| %>
        <%= f.hidden_field :report_id, :value => @report.id %>
        <tr class="highlite grid edit_item_<%= item.id %>">
            <td <%= item.notes? ? "style=border-bottom:none;" : "" %>>

                <%= f.text_field :name, :style => "font-size:1em;width:234px;margin-right:20px;", :value => item.name, :id => "first_focus_#{item.id}" %>

                <%= f.select :tax_id, Tax.all.collect{ |c| 

[c.territory_short_form, c.id]}, {:selected => item.tax_id}, :style => 'font-size:1em;'  %> 
                    </td>
             <tr />
    <% end %>

Update.js.erb:

$("#item_form<%=@item.id%>").html("<%= escape_javascript(render(:partial => 'reports/edit_item_row', :locals => {:item => @item})).html_safe %>");
Was it helpful?

Solution

Add this line to your partial:

<input name="authenticity_token" type="hidden"
                                         value="<%= form_authenticity_token %>"/>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top