设计 gem 和 jquery
我正在使用 devise gem (1.4.2) 和rails (3.0.7)。我有一个带有一些复选框和一些 jquery 设置的视图,这样当我选中一个框时,它会发布一个由我的控制器之一处理的帖子。不幸的是,当我这样做时,它会将当前用户注销。有没有简单的方法可以避免这种情况?谢谢!
jquery代码:
$(‘.do_some_action).live(‘change’, function(){
$.post(‘/my_model/do_some_action’, {param1 : param1Value})
return false
})
基本控制器:
class AuthorizedController < ApplicationController
before_filter :authenticate_user!
check_authorization
load_and_authorize_resource
rescue_from CanCan::AccessDenied do |exception|
flash[:error] = exception.message
redirect_to request.referer
end
end
特定控制器:
class MyModelsController < AuthorizedController
...
def some_action
...
end
end
-Jordan
I'm using the devise gem (1.4.2) and rails (3.0.7). I've got a view with some check boxes and some jquery set up such that when I check a box it does a post which is handled by one of my controllers. Unfortunately, when I do this, it signs the current user out. Is there an easy way to avoid this? Thanks!
jquery code:
$(‘.do_some_action).live(‘change’, function(){
$.post(‘/my_model/do_some_action’, {param1 : param1Value})
return false
})
base controller:
class AuthorizedController < ApplicationController
before_filter :authenticate_user!
check_authorization
load_and_authorize_resource
rescue_from CanCan::AccessDenied do |exception|
flash[:error] = exception.message
redirect_to request.referer
end
end
specific controller:
class MyModelsController < AuthorizedController
...
def some_action
...
end
end
-Jordan
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
听起来您没有在 AJAX 请求中传递 CSFR 令牌。
确保通过在 HTML 中调用
csrf_meta_tag
设置 CSFR 元标记。然后,您可以使用以下 JavaScript 来确保在任何 AJAX 请求上设置 CSFR 标记。您还可以通过在控制器中添加以下内容来禁用该操作的 CSFR 检查,但 JavaScript 替代方案会更好。
Sounds like you aren't passing the CSFR token in your AJAX requests.
Make sure the CSFR meta tag is being set by calling
csrf_meta_tag
in the HTML . Then you can use the below bit of JavaScript to ensure the CSFR tag is set on any AJAX requests.You can also disable the CSFR check for that action by adding the below in your controller, but the JavaScript alternative would be better.