2012-11-13 112 views
0

我有一些看起來像這樣在我的能力類CanCan with block會授權給Class嗎?

def initialize(staff) 
    staff ||= Staff.new 
    can :manage, Store do |store| 
     store.staff_privileges.select(&:owner?).map(&:staff_id).include? staff.id 
    end 
end 

我不知道爲什麼,因爲我覺得上面的塊應該只得到在商店的實例,而不是類執行staff.can? :manage這裏將返回true本身

​​

回答

0

https://github.com/ryanb/cancan/wiki/Defining-Abilities-with-Blocks

The block is only evaluated when an actual instance object is present. It is not evaluated when checking permissions on the class (such as in the index action). This means any conditions which are not dependent on the object attributes should be moved outside of the block. 

爲什麼會變成這樣?我不知道,但我認爲答案是在「如在指數行動」位呢?如果沒有這種行爲,load_and_authorize_resource方法可以提供對索引操作不起作用。