控制器显示动作
def show
@batch = Batch.find(params[:id])
@batch_id = @batch.id
authorize @batch
end
专家政策
def show?
puts @batch_id
if !current_user.nil? && (current_user.role?('Student')) || (current_user.role?('Administrator')) || (current_user.role?('SupportSpecialist')) || (current_user.role?('Instructor'))
true
else
false
end
end
当我puts @batch_id
时,我得到的是零,我如何在政策行动中得到这个值?
发布于 2015-03-04 13:50:33
你的控制器:
def show
@batch = Batch.find(params[:id])
@batch_id = @batch.id
authorize @batch
end
您的策略文件可能是:
class BatchPolicy
attr_reader :user, :record
def initialize(user, record)
@user = user
@record = record
end
def show?
true if record ... // record will be equal @batch
end
end
如果您需要额外的上下文,那么您必须知道:
专家强烈鼓励您对应用程序进行建模,使授权所需的唯一上下文是要检查授权的用户对象和域模型。如果您发现自己需要更多的上下文,请考虑是否授权正确的域模型,也许另一个域模型(或多个域模型的包装器)可以提供所需的上下文。 正是出于这个原因,专家不允许您向策略传递附加参数。
那里你可以从马的嘴里学到它。
https://stackoverflow.com/questions/28855560
复制相似问题