Register now and start sharing your code snippets.
-->
How to use jQuery with Rails 2.0 - aka How to fix "ActionController::InvalidAuthenticityToken (ActionController::InvalidAuthenticityToken)"
Ruby posted 6 months ago by christian
All credit goes to Henrik Nyh for writing a blog post about how to fix this issue.
This is a slight variation of his code:
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 settings.data = settings.data || ""; 4 settings.data += (settings.data ? "&" : "") + "authenticity_token=" + encodeURIComponent(window.AUTH_TOKEN); 5 });
That’s all…
ActionController::InvalidAuthenticityToken (ActionController::InvalidAuthenticityToken):
HTML (Rails) posted 6 months ago by christian
1 <input name="authenticity_token" value="<%= form_authenticity_token %>" type="hidden" />
or
1 <%= token_tag %>