我已经为此苦苦挣扎了一段时间,终于到了 CanCan 似乎不允许您授权记录集合的地步。例如:
ads_controller.rb
def index
@ads = Ad.where("ads.published_at >= ?", 30.days.ago).order("ads.published_at DESC")
authorize! :read, @ads
end
能力.rb
def initialize(user)
user ||= User.new # Guest user
if user
if user.role? :admin # Logged in as admin
can :manage, :all
else # Logged in as general user
can :read, Ad
can :read_own, Ad, :user_id => user.id
can :create, Ad
end
else # Not logged in (Guest)
can :read, Ad
end
end
这会在尝试访问索引操作时产生未经授权的访问消息。
You are not authorized to access this page.
但是,如果您更改 index 操作中的授权调用以检查 Ad 类而不是像这样的集合
def index
@ads = Ad.where("ads.published_at >= ?", 30.days.ago)
authorize! :read, Ad
end
...它工作正常。
任何帮助解释这一点将不胜感激。
提前致谢。
附言。当我试图解决这个问题时,我最初得到了重定向循环。事实证明,有一个带有推荐的 rescue_from 的 gotchya,您将其放入应用程序控制器中,以便为您提供很好的错误消息。如果您的 root_path 设置为您授权的相同位置!call 不正确(或失败),您将获得重定向循环。注释掉rescue_from 学到了一个艰难的方法。