【发布时间】:2011-05-13 09:34:50
【问题描述】:
这是我的问题:
我正在使用 cancan gem。我认为一切都设置得很好,除了一件事,它产生undefined method 'user_type?' for "admin":String。
这是我的能力.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' [...]
我不知道我必须修改什么。为什么if user.user_type? :admin 不检查当前用户属性?非常感谢您的关注!
UP:好的,我读到了cancan需要在应用程序控制器中声明的方法“current_user”。现在我正在努力。新闻,很快。
【问题讨论】:
-
你在哪里定义了user_type?方法?
-
你在哪里检查授权?什么控制器? (如果它是控制器)请也粘贴该代码。这是因为不知何故,传递给
CanCan的user变量是一个值为"admin"的字符串——你希望user实际上是一个User实例。 -
解决了!!!进入ability.rb if user.user_type == "admin" 在应用程序控制器中我添加 def current_user @current_user = User.find_by_id(session[:user_id]) end 谢谢!
标签: ruby-on-rails authorization gem cancan