Created
June 12, 2009 01:47
-
-
Save ahx/128377 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//######### How to send authenticity_token automatically with every Ajax.Request. | |
//######### This will be obsolete in Rails 3 (as Yehuda mentiones here : https://rails.lighthouseapp.com/projects/8994/tickets/2696-prototypehelper-no-auth-token-param-for-get-requests#ticket-2696-2) | |
//######### Put this in your application.js | |
// Monkeypatches Ajax.Base to include authenticity_token parameter unless its already there | |
(function() { | |
function insert_auth_token (options) { | |
var o = options || {}; | |
if (typeof(o.parameters) == "String" && !o.parameters.include("authenticity_token")) { | |
o.parameters = o.parameters + "&authenticity_token="+AUTH_TOKEN; | |
} else { | |
o = Object.extend({ parameters: { authenticity_token: AUTH_TOKEN} }, o); | |
} | |
return o; | |
} | |
// Prototype's Function#wrap something like alias_method_chain | |
Ajax.Base.prototype.initialize = Ajax.Base.prototype.initialize.wrap( | |
function(proceed, options) { | |
return proceed(insert_auth_token(options)); | |
} | |
); | |
})(); | |
//######### Put this in your Layout | |
<%= javascript_tag "var AUTH_TOKEN = #{form_authenticity_token.inspect};" if protect_against_forgery? %> | |
//######### If you don't have to use Protoype, here's how to do this with JQuery http://henrik.nyh.se/2008/05/rails-authenticity-token-with-jquery | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment