Rails3 - CanCan - 未初始化常量 能力::页面
我刚刚将 cancan 1.5.0 添加到我的 Rails 3 应用程序中,这是我的能力文件 -
def initialize(user)
user ||= User.new
if user.role == 'Admin'
can :manage, :all
end
if user.role == 'Standard'
can :manage, Library
can :manage, Page
else
can :manage, Page
can :manage, Library
end
我有一个自定义类(非静态函数)
class PagesController < ApplicationController
authorize_resource :class => false
def home
end
end
如您所见,我正在为非静态类使用正确的函数,但我仍然得到这个错误 -
uninitialized constant Ability::Page
这是堆栈跟踪的开头 -
app/models/ability.rb:16:in `initialize'
cancan (1.5.0) lib/cancan/controller_additions.rb:327:in `new'
cancan (1.5.0) lib/cancan/controller_additions.rb:327:in `current_ability'
cancan (1.5.0) lib/cancan/controller_additions.rb:308:in `authorize!'
cancan (1.5.0) lib/cancan/controller_resource.rb:40:in `authorize_resource'
cancan (1.5.0) lib/cancan/controller_resource.rb:9:in `block in add_before_filter'
activesupport (3.0.3) lib/active_support/callbacks.rb:436:in ` _run__1386450187816505438__process_action__15559788756486462__callbacks'
activesupport (3.0.3) lib/active_support/callbacks.rb:409:in `_run_process_action_callbacks'
activesupport (3.0.3) lib/active_support/callbacks.rb:93:in `run_callbacks'
谢谢,亚历克斯
I have just added cancan 1.5.0 to my rails 3 app here is my ability file -
def initialize(user)
user ||= User.new
if user.role == 'Admin'
can :manage, :all
end
if user.role == 'Standard'
can :manage, Library
can :manage, Page
else
can :manage, Page
can :manage, Library
end
I have a custom class (non-restful functions)
class PagesController < ApplicationController
authorize_resource :class => false
def home
end
end
As you can see I am using the correct function for a not restful class but I am still getting this error -
uninitialized constant Ability::Page
Here is the beginning of the stacktrace -
app/models/ability.rb:16:in `initialize'
cancan (1.5.0) lib/cancan/controller_additions.rb:327:in `new'
cancan (1.5.0) lib/cancan/controller_additions.rb:327:in `current_ability'
cancan (1.5.0) lib/cancan/controller_additions.rb:308:in `authorize!'
cancan (1.5.0) lib/cancan/controller_resource.rb:40:in `authorize_resource'
cancan (1.5.0) lib/cancan/controller_resource.rb:9:in `block in add_before_filter'
activesupport (3.0.3) lib/active_support/callbacks.rb:436:in ` _run__1386450187816505438__process_action__15559788756486462__callbacks'
activesupport (3.0.3) lib/active_support/callbacks.rb:409:in `_run_process_action_callbacks'
activesupport (3.0.3) lib/active_support/callbacks.rb:93:in `run_callbacks'
Thanks, Alex
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
CanCan 文档将
can
方法描述为:因此,问题在于您的系统中没有供 CanCan 管理访问的
Page
类。请注意,CanCan 的构建如下:(重点是我添加的)
因此,如果您的目标是控制没有附加 Rails 资源的抽象概念,那么您可能会很难使用 CanCan
The CanCan documentation describes the
can
method as:So, the problem is that you don't have a
Page
class in your system for CanCan to manage access to.Note that CanCan is built as: (emphasis added by me)
So if you are aiming to control abstract concepts which don't have rails resources attached to them then you'll probably have a tough time with CanCan
请注意现在发现此内容的任何人...
您可以授权任何非静态控制器、抽象类和方法..
示例:
/app/models/role_ability.rb
:do_this 和 :on_this 完全是任意的,但它们必须匹配授权!控制器中的参数就像这样...
只要记住,您可能已经在 ApplicationController 中进行了一些资源授权,也许像这样
,所以请记住在非静态/抽象控制器中跳过_authorize_resource
现在管理员可以 :do_this, :on_this并会很好地授权。您可能想要更语义地命名该能力,只是想强调任意性。
这都是使用Cancan 1.5,之前没有尝试过任何东西。
来自 https://github.com/ryanb/cancan/wiki/Non-RESTful-Controllers
Just a note to anyone finding this now on...
You can authorise any non-restful controller, abstract classes and methods..
Example:
/app/models/role_ability.rb
:do_this and :on_this are completly arbitrary but they must match the authorize! params in the controller like so...
Just remember that chances are you probably already have some resource authorization happening from within the ApplicationController maybe like this
so remember to skip_authorize_resource in your non-restful/abstract controller
Now an admin can :do_this, :on_this and will authorize nicely. You would probably want to name the ability a bit more semantically, just wanted to emphasize the arbitrary-ness.
This is all using Cancan 1.5, haven't tried on anything earlier.
From https://github.com/ryanb/cancan/wiki/Non-RESTful-Controllers