0

私の能力クラスにこんな感じのものがあります

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上記のブロックはクラス自体ではなく store のインスタンスでのみ実行されるべきだと思ったので、ここで true を返す理由がわかりません

staff = Staff.first
staff.can? :manage, Store #true
staff.can? :manage, Store.first #false, because there is no staff_privileges associated to this store
4

1 に答える 1

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.

なぜこれでしょうか?わかりませんが、答えはそこにある「インデックスアクションなど」のビットにあると思いますか?その動作がないと、cancanが提供できるload_and_authorize_resourceメソッドはインデックスアクションでは機能しません。

于 2012-11-13T02:12:26.277 に答える