Skip to content

Instantly share code, notes, and snippets.

@spohlenz
Created August 11, 2008 18:17
Show Gist options
  • Save spohlenz/4912 to your computer and use it in GitHub Desktop.
Save spohlenz/4912 to your computer and use it in GitHub Desktop.
Integrate jQuery with Rails' forgery protection and respond_to
<head>
<%= define_js_authenticity_token %>
</head>
$.ajaxSetup({
data: { authenticity_token: AUTHENTICITY_TOKEN },
beforeSend: function(xhr) {
xhr.setRequestHeader("Accept", "text/javascript")
}
});
def define_js_authenticity_token
javascript_tag("var AUTHENTICITY_TOKEN = '#{form_authenticity_token}';") if protect_against_forgery?
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment