如何从 pundit 中的特定策略授权 (Rails)

How to authorize from an especific policy in pundit (Rails)

有没有办法在Punditauthorization方法中指定策略class?当你这样做时

    authorize @user, :show

它使用 UserPolicy class,因为 @user 是一个 User(模型)实例。有人知道在另一个策略 class 上执行 authorize 方法的方法吗?像 CustomerPolicy,不存在 Customer 模型 class。

您可以使用符号而不是模型实例来调用 "headless" 策略(没有支持模型的策略)。

authorize :customer, :show
# or for a namespaced policy
authorize [:people, :customer] 

另一种选择是在模型上设置策略 class:

class User < ActiveRecord::Base
  def self.policy_class
    CustomerPolicy
  end
end