2016-04-15 17 views
0

punditを使って権限を与えてDeviseから認証しようとすると、このエラーが発生します。Pundit ::引数の数が間違っています(0の場合は1)

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 

私は

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では、次のようにapplication_policy.rbを持っていますこれをすること

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

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

    ... 

end 

なぜこのエラーが発生するのですか。今の私の頭を壊している。

+0

TripPolicy.new(current_user、@trip).new?が動作しない限り、私は 'raise"を許可していません。もし私が 'Pundit.authorize current_user、@trip、"を使用した場合、new? "は動作します。しかし、私はちょうど直接メソッドを使用すると、その混乱する方法。 –

答えて

0

問題を解明しました。私のコードには問題がありました。 authorize関数内では、パラメータを受け入れるPolicy関数を参照します。

私のapplication_controller.rbには、問題を引き起こしていたパラメータを受け入れなかった別のポリシー機能がありました。ポリシー機能の名前を変更して問題を解決しました。

時には問題を寝返りにして、翌日起床して新しく起きることがあります。

関連する問題