2012-05-08 79 views
0

分配創建軌道上現場紅寶石和最近增加。我認爲這可能與我的能力有關:找不到關聯關係:在模型用戶

class Ability 
    include CanCan::Ability 
    def initialize(user) 
     user ||= User.new 
     can :read, :all 
     if user.role? "admin" 
     can :manage, :all 
    end 
    def initialize(user) 
     user ||= User.new 
     can :read, :all 
     if user.role? "coach" 
     can :manage, :all 
    end 
    def initialize(user) 
     user ||= User.new 
     can :read, :all 
     if user.role? "captain" 
     can :manage, :tournaments 
     can :manage, :results 
    end 
    def initialize(user) 
     user ||= User.new 
     can :read, :all 
     if user.role? "teammember" 
     can :manage, :individualresults 
    end 
    end 
end 

感謝任何幫助。如果你想要額外的代碼讓我知道。

+2

你應該* *絕對沒有超過一個'initialize'方法。您最後還會看到額外的「結尾」。 –

+0

所以我將角色分配給某些用戶,這是「if user.role?」團隊成員「」等進入的地方。額外的「結束」現在消失了,但我認爲它不會影響任何事情。 – user1378701

+0

我應該將所有這些組合成一個初始化方法嗎? – user1378701

回答

2

Your ability file大概應該是這樣的:

class Ability 
    include CanCan::Ability 
    def initialize(user) 
    user ||= User.new 
    can :read, :all 
    if (user.role? "admin" || user.role? "coach") 
     can :manage, :all 
    end 
    if user.role? "captain" 
     can :manage, Tournament 
     can :manage, Result 
    end 
    if user.role? "teammember" 
     can :manage, Individualresult 
    end 
    end 
end 

Checking a user action against the defined CanCan rules就像做:

if can?(:create, Table) 
+0

非常感謝,完美的工作。我還發現我的角色中缺少一種關係。 – user1378701