2015-08-15 65 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