I have an api setup in my rails application, and after creating my first form and trying to post something I encountered this error:
Can't verify CSRF token authenticity.
Completed 422 Unprocessable Entity in 2ms (ActiveRecord: 0.0ms)
ActionController::InvalidAuthenticityToken - ActionController::InvalidAuthenticityToken:
I have read plenty of posts that recommended things like this:
$.ajaxSetup({
headers: {
'X-Transaction': 'POST Example',
'X-CSRF-Token': $('meta[name="csrf-token"]').attr('content')
},
})
or passing form_authenticity_token directly to my javascript (as this is a remote form), but nothing works. Still the same error.
Does anyone know how to solve it?
skip_before_action :verify_authenticity_token. Using which authentication method for the API is a case to case thing and you should search and find one suitable for your project.