Permalink
Browse files

Use jquery_ujs to send CSRF token dynamically

  • Loading branch information...
tricknotes committed Jan 22, 2015
1 parent d3a0192 commit 05ccd1f4c47a80d78ed1ff6ca210a32bc9322b36
@@ -325,6 +325,22 @@ or for ember-data
rails generate ember:install --tag=v1.0.0-beta.2 --ember-data
## CSRF Token
Rails [`protect_from_forgery`](http://api.rubyonrails.org/classes/ActionController/RequestForgeryProtection/ClassMethods.html#method-i-protect_from_forgery) requires CSRF token for every XHR except GET.
The CSRF token is normally found in `app/views/layouts/application.html.*` inserted with the rails helper: [`csrf_meta_tags`](http://api.rubyonrails.org/classes/ActionView/Helpers/CsrfHelper.html#method-i-csrf_meta_tags).
When you use [jquery-ujs](https://github.com/rails/jquery-ujs),
the CSRF token will be send to rails application on every XHR automatically.
If not so, the following JavaScript is required in your code.
``` javascript
$.ajaxPrefilter(function(options, originalOptions, xhr) {
var token = $('meta[name="csrf-token"]').attr('content');
xhr.setRequestHeader('X-CSRF-Token', token);
});
```
## Note on Patches/Pull Requests
1. Fork the project.
@@ -38,10 +38,6 @@ def create_router_file
template "router.#{engine_extension}", "#{ember_path}/router.#{engine_extension}"
end
def create_store_file
template "store.#{engine_extension}", "#{ember_path}/store.#{engine_extension}"
end
def create_adapter_file
template "application_adapter.#{engine_extension}", "#{ember_path}/adapters/application_adapter.#{engine_extension}"
end
@@ -1,4 +1,3 @@
//= require ./store
//= require_tree ./adapters
//= require_tree ./mixins
//= require_tree ./models
@@ -1,4 +1,3 @@
#= require ./store
#= require_tree ./adapters
#= require_tree ./mixins
#= require_tree ./models
@@ -1,4 +1,3 @@
#= require ./store
#= require_tree ./adapters
#= require_tree ./mixins
#= require_tree ./models
@@ -1,4 +1,5 @@
//= require jquery
//= require jquery_ujs
//= require handlebars
//= require ember
//= require ember-data
@@ -1,4 +1,5 @@
#= require jquery
#= require jquery_ujs
#= require handlebars
#= require ember
#= require ember-data
@@ -1,4 +1,5 @@
#= require jquery
#= require jquery_ujs
#= require handlebars
#= require ember
#= require ember-data

This file was deleted.

Oops, something went wrong.

This file was deleted.

Oops, something went wrong.

This file was deleted.

Oops, something went wrong.
@@ -43,7 +43,6 @@ def cleanup
test "create bootstrap in a rails engine with #{engine}" do
run_generator ["--javascript-engine=#{engine}"]
assert_file "#{ember_path}/store.js.#{engine}".sub('.js.js','.js')
assert_file "#{ember_path}/router.js.#{engine}".sub('.js.js','.js')
assert_file "#{ember_path}/adapters/application_adapter.js.#{engine}".sub('.js.js','.js')
assert_file "#{ember_path}/#{engine_name}.js.#{engine}".sub('.js.js','.js')
@@ -52,7 +51,6 @@ def cleanup
test "create bootstrap in a rails engine with #{engine} engine and custom path" do
custom_path = ember_path("custom")
run_generator ["--javascript-engine=#{engine}", "-d", custom_path]
assert_file "#{custom_path}/store.js.#{engine}".sub('.js.js','.js')
assert_file "#{custom_path}/router.js.#{engine}".sub('.js.js','.js')
assert_file "#{custom_path}/adapters/application_adapter.js.#{engine}".sub('.js.js','.js')
assert_file "#{custom_path}/#{engine_name}.js.#{engine}".sub('.js.js','.js')
@@ -36,7 +36,6 @@ def prepare_destination
test "create bootstrap with #{engine} engine" do
run_generator ["--javascript-engine=#{engine}"]
assert_file "#{ember_path}/store.js.#{engine}".sub('.js.js','.js')
assert_file "#{ember_path}/router.js.#{engine}".sub('.js.js','.js')
assert_file "#{ember_path}/adapters/application_adapter.js.#{engine}".sub('.js.js','.js')
assert_file "#{ember_path}/#{application_name.underscore}.js.#{engine}".sub('.js.js','.js')
@@ -47,7 +46,6 @@ def prepare_destination
test "create bootstrap with #{engine} engine and custom path" do
custom_path = ember_path("custom")
run_generator ["--javascript-engine=#{engine}", "-d", custom_path]
assert_file "#{custom_path}/store.js.#{engine}".sub('.js.js','.js')
assert_file "#{custom_path}/router.js.#{engine}".sub('.js.js','.js')
assert_file "#{custom_path}/adapters/application_adapter.js.#{engine}".sub('.js.js','.js')
assert_file "#{custom_path}/#{application_name.underscore}.js.#{engine}".sub('.js.js','.js')
@@ -112,7 +110,6 @@ def prepare_destination
old, ::Rails.configuration.ember.ember_path = ::Rails.configuration.ember.ember_path, custom_path
run_generator
assert_file "#{custom_path}/store.js"
assert_file "#{custom_path}/router.js"
assert_file "#{custom_path}/#{application_name.underscore}.js"
ensure
@@ -135,7 +132,6 @@ def assert_invoked_generators_files(options = {})
assert_file "#{path}/application.js"
assert_file "#{path}/#{application_name}.js"
assert_file "#{path}/router.js"
assert_file "#{path}/store.js"
assert_file "#{path}/adapters/application_adapter.js"
end

0 comments on commit 05ccd1f

Please sign in to comment.