0
有没有方法在Pundit
的authorization
方法中指定策略类?当你做如何通过专家策略中的专家授权(Rails)
authorize @user, :show
它使用UserPolicy
类,因为@user
是User
(模型)的实例。有人知道在另一个策略类上执行authorize
方法的方法吗?如CustomerPolicy
,不存在Customer
模型类。
有没有方法在Pundit
的authorization
方法中指定策略类?当你做如何通过专家策略中的专家授权(Rails)
authorize @user, :show
它使用UserPolicy
类,因为@user
是User
(模型)的实例。有人知道在另一个策略类上执行authorize
方法的方法吗?如CustomerPolicy
,不存在Customer
模型类。
您可以使用符号而不是模型实例来调用“无头”策略(没有支持模型的策略)。
authorize :customer, :show
# or for a namespaced policy
authorize [:people, :customer]
另一种选择是设置模型类政策:
class User < ActiveRecord::Base
def self.policy_class
CustomerPolicy
end
end
https://github.com/elabs/pundit#headless-policies – max
不错,就在那里(小白)。只需补充说明,如果策略是命名空间的,那么您需要为命名空间提供一个符号数组,如果您拥有“People :: CustomerPolicy”,那么授权将是'authorize [:people,:customer]'。如果帮助某人:P – mariowise