1
我需要爲用戶添加一個過濾器。只有當用戶屬於同一分支時,用戶才能看到其他用戶。一個用戶可以屬於多個分支。使用CanCan和Ruby on Rails的動作索引塊定義功能
我正在用下面的塊定義能力。但在提到文檔並面對錯誤後,我知道index
方法不適合定義block的能力。
#This code is part of initialisation method of ability.rb
can :index, User do |curr_user|
#check if user belongs to same branch
!curr_user.branch_ids.index(branch_id).nil?
end
這裏是用戶控制器
def index
authorize! :index, User
@users = User.where(params[:user]).accessible_by(current_ability, :index)
renders :obj =>@users
end
的索引方法什麼是一些可替換定義爲執行地方定義與塊的能力這一邏輯能力?