ruby-on-rails – 具有两个输入参数的Pundit策略
发布时间:2020-12-16 22:02:22 所属栏目:百科 来源:网络整理
导读:我和Rails很新,我对以下政策有问题(使用 Pundit):我想比较两个对象:@record和@foo,如下所示: class BarPolicy ApplicationPolicy def show? @record.foo_id == @foo endend 我找不到找到一个很好的方法来传递第二个参数到pundit方法(@foo). 我想做一些像
我和Rails很新,我对以下政策有问题(使用
Pundit):我想比较两个对象:@record和@foo,如下所示:
class BarPolicy < ApplicationPolicy def show? @record.foo_id == @foo end end 我找不到找到一个很好的方法来传递第二个参数到pundit方法(@foo). 我想做一些像: class BarsController < ApplicationController def test authorize bar,@foo,:show? # Throws ArgumentError ... end end 但是Pundit授权方法只允许两个参数. 谢谢! 解决方法
我在
here找到了答案.
这是我的方式: 在ApplicationController中添加pundit_user函数: class ApplicationController < ActionController::Base include Pundit def pundit_user CurrentContext.new(current_user,foo) end 创建CurrentContext类: /lib/pundit/current_context.rb class CurrentContext attr_reader :user,:foo def initialize(user,foo) @user = user @foo = foo end end 更新初始化Pundit方法. class ApplicationPolicy attr_reader :user,:record,:foo def initialize(context,record) @user = context.user @foo = context.foo @record = record end end (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |