[英]Rails 4 exception from CoffeeScript: ActionController::InvalidAuthenticityToken
I have ActionController::InvalidAuthenticityToken when trying to submit dynamically created form from coffeescript. 尝试从coffeescript提交动态创建的表单时,我具有ActionController :: InvalidAuthenticityToken。
I tried to pass authenticity_token in my form request, but had no success. 我试图在表单请求中传递authenticity_token,但没有成功。 Can you please help me?
你能帮我么?
My CoffeeDcript code; 我的CoffeeDcript代码;
upload_form = $('<form>', {
'action': '/save_orders',
'method': 'post'
}).append($('<input>', {
'data': my_json
}));
upload_form.submit();
Please note, that i neeed to create and submit form dynamically, so i can't prepare it on html page and then reuse in js. 请注意,我需要动态创建和提交表单,因此我无法在html页面上准备表单,然后在js中重用。 Also it should be form request (not ajax)
也应该是表单请求(不是ajax)
Somewhere in a rails view (eg application.html.erb file) you can set a global js variable like this: 您可以在Rails视图中的某个位置(例如application.html.erb文件)设置全局js变量,如下所示:
window._token = '<%= form_authenticity_token %>';
Then when create a form you can append it to the form like this: 然后,在创建表单时,可以将其追加到表单中,如下所示:
upload_form = $('<form>', {
'action': '/save_orders',
'method': 'post'
}).append($('<input>', {
'data': my_json
})).append($('<input>', {
'type': 'hidden',
'name': 'authenticity_token',
'value': window._token
}));
upload_form.submit();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.