0

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 %>");
fatfrog
  • 2,118
  • 1
  • 23
  • 46

1 Answers1

1

Add this line to your partial:

<input name="authenticity_token" type="hidden"
                                         value="<%= form_authenticity_token %>"/>
Chandranshu
  • 3,669
  • 3
  • 20
  • 37
  • That did it! Thanks! Is there a reason Rails can't figure out it's supposed to be a put? – fatfrog Nov 21 '13 at 05:17
  • 1
    Rails does figure it out for the first response but when you are sending partials, rails has trouble figuring out that you have included a form. Ideally, I'd also like rails to figure that out. BTW, don't forget to accept the answer. – Chandranshu Nov 21 '13 at 05:18
  • StackOverflow makes you wait 5 minutes before you can mark an answer, huge pain in the butt. Thanks again! – fatfrog Nov 21 '13 at 05:21