Note that there are some explanatory texts on larger screens.

plurals
  1. PORails 3: CSRF with form_tag
    primarykey
    data
    text
    <p><strong>QUICK BACKGROUND</strong></p> <p>I have a model where a <code>facility</code> has_many <code>managers</code> through <code>relationships</code>. The relationship model holds one additional attribute, <code>admin</code>, which is a boolean. Then, for each facility, I have a page that allows an admin/some user to mark <code>managers</code>'s as <code>admin</code> or not. They do this through checkboxes. I am using devise for managers. Everything is pretty much done like the first 2 minutes of this railscast:</p> <p><a href="http://railscasts.com/episodes/165-edit-multiple-revised?view=comments" rel="nofollow">http://railscasts.com/episodes/165-edit-multiple-revised?view=comments</a></p> <p><strong>THE PROBLEM(S)</strong></p> <p>The issue is, when I submit the form_tag, I get:</p> <p><strong>1.</strong> WARNING: Can't verify CSRF token authenticity</p> <p><strong>2.</strong> It then resets my user session (logs me out of devise), so I have to log back in to continue working.</p> <p>If I remove <code>protect from forgery</code> from the application controller, everything works smoothly but obviously I don't want to do that as my fix.</p> <p>So, how can I avoid this CSFR issue with form_tag? </p> <p><strong>THE FORM</strong> (updated)</p> <pre><code>&lt;%= form_tag admin_relationships_path, method: :put do %&gt; &lt;table&gt; &lt;tr&gt; &lt;th&gt;&lt;/th&gt; &lt;th&gt;Person Name&lt;/th&gt; &lt;th&gt;Admin&lt;/th&gt; &lt;/tr&gt; &lt;% @relationships.each do |relationship| %&gt; &lt;tr&gt; &lt;td&gt;&lt;%= check_box_tag "relationship_ids[]", relationship.id %&gt;&lt;/td&gt; **&lt;%= hidden_field_tag :authenticity_token, form_authenticity_token %&gt;** &lt;td&gt;&lt;%= User.find(relationship.user_id).full_name %&gt;&lt;/td&gt; &lt;td&gt;&lt;%= relationship.admin ? "Yes" : "No" %&gt;&lt;/td&gt; &lt;td&gt;&lt;%= link_to 'Remove', relationship, method: :delete, **remote: true** %&gt;&lt;/td&gt; &lt;/tr&gt; &lt;% end %&gt; &lt;/table&gt; &lt;%= submit_tag "Make Checked Admins" %&gt; &lt;% end %&gt; </code></pre> <p>The contoller.</p> <pre><code>class RelationshipsController &lt; ApplicationController def destroy @relationship = Relationship.find(params[:id]) @relationship.destroy respond_to do |format| format.html { redirect_to "/facilities/1679/invite", notice: " Manager has been deleted." } format.json { head :no_content } end end </code></pre> <p>The log.</p> <pre><code>Started DELETE "/relationships/52" for 127.0.0.1 at 2013-11-26 14:30:11 -0500 Processing by RelationshipsController#destroy as JS Parameters: {"id"=&gt;"52"} WARNING: Can't verify CSRF token authenticity User Load (0.3ms) SELECT `users`.* FROM `users` WHERE `users`.`id` = 38 LIMIT 1 (0.1ms) BEGIN (0.1ms) COMMIT Relationship Load (0.2ms) SELECT `relationships`.* FROM `relationships` WHERE `relationships`.`id` = 52 LIMIT 1 (0.1ms) BEGIN SQL (0.2ms) DELETE FROM `relationships` WHERE `relationships`.`id` = 52 (0.4ms) COMMIT Redirected to http://localhost:3000/facilities/1679/invite Completed 302 Found in 10ms (ActiveRecord: 1.6ms) Started DELETE "/facilities/1679/invite" for 127.0.0.1 at 2013-11-26 14:30:11 -0500 ActionController::RoutingError (No route matches [DELETE] "/facilities/1679/invite"): </code></pre> <p>Request Headers:</p> <p><code>Accept</code>, <code>Accept-Encoding</code>, <code>Accept-Language</code>, <code>Connection</code>, <code>Cookie</code>, <code>Host</code>, <code>Origin</code>, <code>Referer</code>, <code>User-Agent</code>, <code>X-Requested-With</code></p> <p><strong>THE ANSWER</strong></p> <p>As the poster below mentioned, my CSFR token was not being received on the page. Although, I had the <code>&lt;%= csrf_meta_tag %&gt;</code> in my application layout and admin layout, and I was actually rendering the application layout, the form was on a Devise page.</p> <p>So I needed to place the <code>&lt;%= csrf_meta_tag %&gt;</code> code in <code>devise.html.erb.</code> You can find the troubleshooting below.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload