Gem devise+ rolif + cancan 问题

lhuang7 · 2013年01月09日 · 最后由 lhuang7 回复于 2013年01月10日 · 5694 次阅读

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。。跟我写重了。已删掉。。。非常感谢大神。。

需要 登录 后方可回复, 如果你还没有账号请 注册新账号