【发布时间】:2016-04-15 03:57:47
【问题描述】:
当我尝试通过权威人士授权并通过设计进行身份验证时,我收到此错误。
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
任何指针为什么我会收到此错误。一段时间以来,我一直在头疼。
【问题讨论】:
-
如果我使用
raise "not authorized" unless TripPolicy.new(current_user, @trip).new?它可以工作。如果我使用Pundit.authorize current_user, @trip, "new?"它可以工作。但是当我直接使用该方法时,它会变得有些困惑。