CanCan 的accessible_by(获取记录)中的current_ability 是什么?
在 CanCan 的文档中,它显示了如何以这种方式获取所有可访问的记录 :
@articles = Article.accessible_by(current_ability)
但是current_ability
是什么?我尝试传递用于身份验证和授权的当前用户,但出现此错误:
NoMethodError: undefined method `conditions' for #<User:0x1092a3b90>
有什么想法应该传递给 accessible_by
或者这里出了什么问题吗?
In the documentation of CanCan it shows how to fetch all accessible records in this way:
@articles = Article.accessible_by(current_ability)
but what is current_ability
? I've tried passing the current user which I'm using for authentication and authorization, but I've got this error:
NoMethodError: undefined method `conditions' for #<User:0x1092a3b90>
Any ideas what should I pass to accessible_by
or what's wrong here?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
CanCan 使用
current_ability
方法(以及其他方法)扩展ActionController::Base
,这是您的ApplicationController
应该继承的类。因此,您问题中的示例代码应该按原样工作。CanCan extends
ActionController::Base
with thecurrent_ability
method (among others), which is the class that yourApplicationController
should inherit from. So the example code in your question should work as is.