Gem devise+ rolif + cancan 问题

lhuang7 · January 09, 2013 · Last by lhuang7 replied at January 10, 2013 · 5710 hits

devise 的 current_user method 跟 cancan。。。。rolify。。加起来就 return error 了

undefined method `current_user' for #Class:0x007f6bb4545688

app/models/user.rb:20:in `has_role?' app/models/ability.rb:11:in `initialize' app/controllers/application_controller.rb:35:in `new' app/controllers/application_controller.rb:35:in `current_ability'

然后 user role。。

def has_role?(role) case role when :admin return true if User.current_user.admin? return false when :member return true else false end end

求大神们解救。。

def has_role?(role)
  case role
    when :admin
      return true if current_user.admin? #current_user 不是User model的方法。
      return false
    when :member
       return true
    else
     false
   end
end

不过我记着 rolify 自己有 has_role 方法了。不需要自己定义吧。 直接可以用比如:

rails c
user = User.first
user.has_role? :admin
#这样就可以。

return true if User.current_user.admin?

这一行写错了,改成:

return true if current_user.admin?

#1 楼 @sandybeauty 是 d。。这是别人写 d。。跟我写重了。已删掉。。。非常感谢大神。。

You need to Sign in before reply, if you don't have an account, please Sign up first.