2011-06-28 15 views
0

に私が持っているリソースネストされたリソースはカンカン

resources :companies do 
    resources :stands 
end 

と私は会社のスタンドへのアクセスを制御したいです。 Abilityクラスiで書く

can :manage, :all if user.has_role? Role.super_admin 

    can :manage, Company do |c| 
     user.has_role? Role.company_admin, c 
    end 

私は会社のスタンドへのアクセスをどのように制御できますか?たとえば、会社管理者は自分の会社のスタンドだけを検索できます。ありがとうございます

答えて

0

Cancanは私たちの行動の許可を設定するのに役立ちます。 Ability.rb /スタンド/検索

def search 
    authorize! :search, Stand 
    current_user.company.search_stands('some-query') # This will get the stands only for the current-users's company 
end 

することができます:

URL:それは文句を言わないの権限に基づいてレコードはそれをAbility.rb

例を設定し、あなたをフェッチ検索、 ユーザースタンド.has_role? Role.company_admin エンド

その他の例: /企業/ 1 /スタンド/検索

def search 
    @company = Company.find(params[:company_id]) 
    authorize! :search_stands, @company 
    @stands = @company.search_stands('some-query') 
end 

Ability.rb

can :search_stands, Company do |c| 
    user.has_role? Role.company_admin # Only admin has the permission to search stands. 
end 
+0

https://github.com/ryanb/cancan/wiki /ネストされたリソース これはどうですか、それは私を助けることができますか? – maxfry

関連する問題