2015-03-31 62 views
0

这是我的能力和我有限的用户,所以他们只能阅读他们自己的。但是如果我经过路线,我仍然可以看到其他用户的索引。cancan方法和设计

我不想将当前用户放在索引路由中,因为我将限制管理用户,因为我必须对模型,管理员和用户进行限制。

class Ability 
     include CanCan::Ability 

     def initialize(user) 

     if user.is_a?(Admin) 

      can :manage, :all 

     elsif user.is_a?(User) 

      can :show, Profile 

      can :read, Profile do |profile| 
      profile.try(:user) == user 
      end 
      can :update, Profile do |profile| 
      profile.try(:user) == user 
      end 
      can :destroy, Profile do |profile| 
      profile.try(:user) == user 
      end 
      can :create, Profile 

     else 

      can :show, Profile 
      cannot :destroy 
      cannot :create 

     end 
     end 
    end 

回答

0

反映并添加限制,以查看用户和配置文件索引页。

class Ability 
     include CanCan::Ability 

     def initialize(user) 

     if user.is_a?(Admin) 

      can :manage, :all 

     elsif user.is_a?(User) 
      can [:show, :create], Profile 
      can [:read, :update, :destroy], Profile, user: user 
      cannot :index, Profile 
      cannot :index, User 
     else 
      can :show, Profile 
      cannot :destroy 
      cannot :create 

     end 
     end 
    end 

你将不得不做的只是重定向用户访问被拒绝的异常。 Take a look into docs