【问题标题】:Pundit :: wrong number of arguments (1 for 0)Pundit :: 参数数量错误(1 代表 0)
【发布时间】: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?" 它可以工作。但是当我直接使用该方法时,它会变得有些困惑。

标签: ruby-on-rails-4 pundit


【解决方案1】:

解决了这个问题。这是我的代码的问题。在授权函数内部,它指的是接受参数的策略函数。

在我的 application_controller.rb 中,我有另一个不接受任何参数的策略函数,这导致了问题。将我的策略函数重命名为其他名称解决了这个问题。

有时候,熬过这个问题并在第二天醒来重新开始是值得的。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-07-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多