CANCAN 未定义方法“user_type?”对于“admin”:字符串
这是我的问题:
我正在使用 cancan gem。我认为一切都设置得很好,除了一件事,它会产生未定义的方法“user_type?”对于“admin”:字符串。
这是我的能力.rb
class Ability
include CanCan::Ability
def initialize(user)
user ||= User.new # guest user
if user.user_type? :admin
can :manage, :all
# [code code code code commented to find more easily the problem]
else
can :read, :all
end
end
end
我的用户.rb
require 'digest/sha2'
class User < ActiveRecord::Base
#definisco i ruoli
ROLES = %w[admin client banned]
default_scope :order => 'user' [...]
我不知道我必须修改什么。为什么如果是user.user_type? :admin
不检查当前用户属性?非常感谢您的关注!
UP:好的,我读到 cancan 需要在应用程序控制器中声明方法“current_user”。现在我正在努力。消息,快了。
here is my problem:
I'm using cancan gem. I think all is set up well, except for one thing, that produces undefined method 'user_type?' for "admin":String
.
Here is my ability.rb
class Ability
include CanCan::Ability
def initialize(user)
user ||= User.new # guest user
if user.user_type? :admin
can :manage, :all
# [code code code code commented to find more easily the problem]
else
can :read, :all
end
end
end
My user.rb
require 'digest/sha2'
class User < ActiveRecord::Base
#definisco i ruoli
ROLES = %w[admin client banned]
default_scope :order => 'user' [...]
I can't figure out what I have to modify. Why if user.user_type? :admin
don't check the current user attribute? Thanks a lot for your attention!
UP: Ok, I read around that cancan need method "current_user" declared in application controller. Now I work on it. News, soon.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你应该这样做。我认为这是更好的方法。
将种子角色植入数据库。
并在应用程序帮助器中粘贴此代码。
然后在能力调用中
,它会正常工作......
You should do like this.I think it is better approach.
Seed roles into DB.
And in application helper paste this code.
And then in ability call like
And it will work fine.......