How to use jQuery with Rails 2.0 - aka How to fix "ActionController::InvalidAuthenticityToken (ActionController::InvalidAuthenticityToken)"

Ruby posted over 2 years ago by christian

This is a slight variation of Henrik Nyh’s code, which fixes an issue with IE6 that makes all Ajax requests use POST in IE6.

In application.html.erb, or whatever layout file you’re using, put:

   1  <%= javascript_tag "window.AUTH_TOKEN = '#{form_authenticity_token}';" %>

In application.js, or whatever JavaScript file you’re using, put:

   1  $(document).ajaxSend(function(event, request, settings) {
   2    if (typeof(window.AUTH_TOKEN) == "undefined") return;
   3    // IE6 fix for http://dev.jquery.com/ticket/3155
   4    if (settings.type == 'GET' || settings.type == 'get') return;
   5  
   6    settings.data = settings.data || "";
   7    settings.data += (settings.data ? "&" : "") + "authenticity_token=" + encodeURIComponent(window.AUTH_TOKEN);
   8  });

That’s all…

Tagged authenticity, rails, rails 2, token, jquery, javascript, ajax

ActionController::InvalidAuthenticityToken (ActionController::InvalidAuthenticityToken):

HTML (Rails) posted over 2 years ago by christian

   1  <input name="authenticity_token" value="<%= form_authenticity_token %>" type="hidden" />

or

   1  <%= token_tag %> 

Tagged authenticity, token, ruby, rails