2015-08-15 66 views
0

有没有方法在Punditauthorization方法中指定策略类?当你做如何通过专家策略中的专家授权(Rails)

authorize @user, :show 

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

+1

https://github.com/elabs/pundit#headless-policies – max

+0

不错,就在那里(小白)。只需补充说明,如果策略是命名空间的,那么您需要为命名空间提供一个符号数组,如果您拥有“People :: CustomerPolicy”,那么授权将是'authorize [:people,:customer]'。如果帮助某人:P – mariowise

回答

1

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

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

另一种选择是设置模型类政策:

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