2015-03-31 40 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