Skip to content

Instantly share code, notes, and snippets.

@benschwarz
Created April 7, 2013 23:11
Show Gist options
  • Save benschwarz/5333009 to your computer and use it in GitHub Desktop.
Save benschwarz/5333009 to your computer and use it in GitHub Desktop.
Set the CSRF token for Rails when doing Ajax requests
define( ['jquery'], function ( $ ) {
var token = $( 'meta[name="csrf-token"]' ).attr( 'content' );
$.ajaxSetup( {
beforeSend: function ( xhr ) {
xhr.setRequestHeader( 'X-CSRF-Token', token );
}
});
return token;
});
Copy link

ghost commented Apr 30, 2018

Can I ask you how to create this cookie in Rails? On ApplicationController maybe? On every NON-GET action?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment