policy_scope
可以完美地找到名为Admin::RemittancePolicy
但authorize
方法的正确政策。
module Admin
class RemittancesController < AdminController # :nodoc:
...
def index
@remittances = policy_scope(Remittance).all
render json: @remittances
end
def show
authorize @remittance
render json: @remittance
end
...
end
end
看一下输出错误:
"#<Pundit::NotDefinedError: unable to find scope `RemittancePolicy::Scope` for `Remittance(...)`>"
也许是pundit的错误,我真的不知道如何解决它。感谢。
以下更多信息:
# policies/admin/admin_policy.rb
module Admin
class AdminPolicy < ApplicationPolicy # :nodoc:
def initialize(user, record)
@user = user
@record = record.is_a?(Array) ? record.last : record
end
def scope
Pundit.policy_scope! user, record.class
end
class Scope # :nodoc:
attr_reader :user, :scope
def initialize(user, scope)
@user = user
@scope = scope.is_a?(Array) ? scope.last : scope
end
def resolve
scope
end
end
end
end
# controllers/admin/admin_controller.rb
module Admin
class AdminController < ActionController::API # :nodoc:
include Knock::Authenticable
include Pundit
before_action :authenticate_user
after_action :verify_authorized, except: :index
after_action :verify_policy_scoped, only: :index
# def policy_scope!(user, scope)
# model = scope.is_a?(Array) ? scope.last : scope
# PolicyFinder.new(scope).scope!.new(user, model).resolve
# end
def policy_scope(scope)
super [:admin, scope]
end
def authorize(record, query = nil)
super [:admin, record], query
end
end
end
答案 0 :(得分:2)
您的stacktrace说错误来自
app/policies/admin/admin_policy.rb:9:in 'scope'
这就是:
def scope
Pundit.policy_scope! user, record.class
end
record.class
评估为Remittance
,因此,如果我了解您要尝试做什么,则需要将scope
更改为
def scope
Pundit.policy_scope! user, [:admin, record.class]
end