[ACCEPTED]-How to turn off rails protect_from_forgery filter only for json-protect-from-forgery
Accepted answer
You can just skip the authenticity token 1 check if its a json request
class ApplicationController < ActionController::Base
skip_before_filter :verify_authenticity_token, if: :json_request?
def json_request?
request.format.json?
end
end
Add the code below to your ./app/controllers/application_controller.rb
:
protect_from_forgery unless: -> { request.format.json? }
0
Instead of disabling the CSRF check you 2 can pass the authenticity_token field in 1 your forms, eg:
<%= hidden_field_tag :authenticity_token, form_authenticity_token %>
Source:
stackoverflow.com
More Related questions
Cookie Warning
We use cookies to improve the performance of the site. By staying on our site, you agree to the terms of use of cookies.