2016-04-15 89 views
0

當我嘗試通過評論者進行授權並通過設計進行身份驗證時,出現此錯誤。Pundit ::錯誤數量的參數(1代表0)

ArgumentError - wrong number of arguments (1 for 0): 
     pundit (1.1.0) lib/pundit.rb:194:in `authorize' 
     app/controllers/trips_controller.rb:23:in `new' 
     actionpack (4.2.5) lib/action_controller/metal/implicit_render.rb:4:in `send_action' 
     actionpack (4.2.5) lib/abstract_controller/base.rb:198:in `process_action' 

我application_controller.rb是

class ApplicationController < ActionController::Base 

    include Pundit 

    protect_from_forgery with: :exception 

    rescue_from Pundit::NotAuthorizedError, with: :permission_denied 
    .... 
end 

有一個application_policy.rb如下

class ApplicationPolicy 
    attr_reader :user, :record 

    def initialize(user, record) 
    @user = user 
    @record = record 
    end 

    def index? 
    false 
    end 

    def show? 
    scope.where(:id => record.id).exists? 
    end 

    def create? 
    false 
    end 

    def new? 
    create? 
    end 

    def update? 
    false 
    end 

    def edit? 
    update? 
    end 

    def destroy? 
    false 
    end 

    def scope 
    Pundit.policy_scope!(user, record.class) 
    end 

    class Scope 
    attr_reader :user, :scope 

    def initialize(user, scope) 
     @user = user 
     @scope = scope 
    end 

    def resolve 
     scope 
    end 
    end 
end 

我trip_policy.rb

class TripPolicy < ApplicationPolicy 
    def new? 
    user.provider? 
    end 

    def create? 
    user.provider? 
    end 

    def update? 
    user.provider? 
    end 

end 

在我trips_controller我是這樣做

class TripsController < ApplicationController 
    before_action :authenticate_user! 
    after_action :verify_authorized, except: [:index, :new] 

    ... 
    def new 
    @trip    = Trip.new 
    authorize @trip 
    end 

    ... 

end 

任何指針爲什麼我得到這個錯誤。一段時間以來,我一直在打破我的頭腦。

+0

如果我使用'raise'未授權「,除非TripPolicy.new(current_user,@trip).new?'有效。如果我使用'Pundit.authorize current_user,@trip,「new?」'它有效。但有些如何直接使用該方法時會感到困惑。 –

回答

0

找出問題所在。這是我的代碼有問題。授權函數內部指的是接受參數的策略函數。

在我的application_controller.rb我有另一個政策功能,不接受任何參數,這是造成問題。將我的策略功能重命名爲別的東西可以解決問題。

有時候,它可以在問題上過夜,並在第二天醒來,重新開始。

相關問題