1

我有一个使用 Devise 和 CanCan 的应用程序。在 config>initializers>Abiliity.rb 类中的能力包括 CanCan::Ability

  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 控制器。基本上,作为普通用户的用户在登录时可以看到项目#index。但并非所有项目都将此用户作为“普通用户”,为什么 cancan 不阻止他的访问?

谢谢

4

1 回答 1

2

确保您按照以下方式调用load_and_authorize_resource您的ProjectsController

class ProjectsController < ApplicationController
  load_and_authorize_resource

  #...
end

如果这仍然不起作用,请尝试authorize!在 index 操作中调用该方法,看看是否有影响,例如:

class ProjectsController < ApplicationController
  #...

  def index
    @projects = Project.all
    authorize! :read, @projects
  end

  #...
end
于 2010-08-27T05:42:19.220 回答