I am using the current beta channel’s ember-data (1.0.0-beta6) in a rails project. Anytime, I post/put/delete data, I get a CSRF exception in rails 4. Obviously, this is because authenticity_token is not be sent in the request. I have the CSRF meta tag with the correct authenticity_token available on the page.
I can bypass the error in my rails controller by adding skip_before_filter :verify_authenticity_token, but I’d prefer to take advantage of the CSRF token check if I can.
I’m guessing there is a way to insert the authenticity_token by doing some magic in:
@jasonmit I try to avoid monkey patching when possible, and extending the ajax function to insert the header seems more brittle in the event that ember-data changes the function’s implementation. Thank you for the suggestion. I always appreciate alternative approaches.
I hear you, but I’ve found it nearly impossible to not extend ember-data’s adapter and serializer. While looking at the adapter again, the better place for this would be in ajaxOptions (line 590) where you can extend the hash object with what I mentioned above and simply call this_super.apply(this, arguments); to avoid any tracking issues with ember-data in the future.
The reason I’d prefer this approach is you’re not overriding all jQuery-based ajax requests and the code is where it belongs, not floating around in your app somewhere.
One big reason that I use $.ajaxPrefilter for things like this is that I want all requests to my server to get the same behaviour anyway. Unless I’m missing something most apps probably do things other than CRUD and use $.ajax directly here and there.
I have an ember-cli project and I am trying to implement the CSRF token authentication to connect to my Rails backend. I have tried everything in this post and have had no luck. I have tried adding the function in various places (script in main index.html, within Active Model Adapter, inside of app.js, etc) and I continue to get Uncaught SyntaxError: Unexpected token < in my console and the following rails errors: