Ruby on rails 如何将rails控制器操作中的实例变量读取到pundit策略
控制器显示动作Ruby on rails 如何将rails控制器操作中的实例变量读取到pundit策略,ruby-on-rails,pundit,Ruby On Rails,Pundit,控制器显示动作 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')) || (cu
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
当我放入@batch_id
时,我得到的是零,我如何在策略操作中获得该值您的控制器:
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
如果需要其他上下文,则必须知道:
Pundit强烈鼓励您以这样的方式对应用程序进行建模:授权所需的唯一上下文是要检查授权的用户对象和域模型。如果你发现自己需要更多的上下文,那么考虑一下你是否授权了正确的域模型,也许另一个域模型(或者围绕多个域模型的包装器)可以提供你需要的上下文。
Pundit不允许您为此向策略传递额外参数。
您可以从马口铁中了解它。您是否在策略中初始化了该方法?