2012-11-13 113 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方法可以提供对索引操作不起作用。