denniss
denniss

Reputation: 17589

Doing AJAX Post in rails without passing in the authenticity_token

So I remember that one time, when i was trying to do an AJAX post, I had to pass in the form_authenticity_token as one of the data to rails. For some reason, not doing so will generate some kind of error and I would get logged out immediately. Is there a way to still have this this authenticity token for form submission but not for ajax post? In other words, I dont want to pass in that autheniticy token in my post data.

Upvotes: 3

Views: 2042

Answers (2)

muirbot
muirbot

Reputation: 2081

Rather than disabling authenticity token verification, you could just pass it to javascript like so:

<%= javascript_tag "var AUTH_TOKEN = #{form_authenticity_token.inspect};" if protect_against_forgery? %>

Then just submit AUTH_TOKEN along with any AJAX posts.

Source

Upvotes: 2

Rob Di Marco
Rob Di Marco

Reputation: 44952

Per the documentation

You can disable csrf protection on controller-by-controller basis:

skip_before_filter :verify_authenticity_token

Upvotes: 4

Related Questions