我有一个使用Devise和CanCan的应用程序。在config>initializers>Abiliity.rb类中,能力包括CanCan::CanCan
def initialize(user)
if user.is? :superadmin
can :manage, :all
elsif user.is? :user
can :read, Project do |project|
project && project.users.include?(user)
end
end
end
end我对项目控制器的指标作用有问题,项目控制器是一个普通的库存RESTful控制器。基本上,普通用户在登录时可以看到projects#index。但是并不是所有的项目都有这个用户作为“普通用户”,为什么cancan没有阻止他的访问呢?
谢谢
发布于 2010-08-27 13:42:19
确保在ProjectsController中调用load_and_authorize_resource,如下所示:
class ProjectsController < ApplicationController
load_and_authorize_resource
#...
end如果这仍然不起作用,请尝试在索引操作中调用authorize!方法,看看这是否有区别,例如:
class ProjectsController < ApplicationController
#...
def index
@projects = Project.all
authorize! :read, @projects
end
#...
endhttps://stackoverflow.com/questions/3580556
复制相似问题