首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Pundit ::错误的参数数量(0对应1)

Pundit ::错误的参数数量(0对应1)
EN

Stack Overflow用户
提问于 2016-04-15 11:57:47
回答 1查看 570关注 0票数 0

当我试图通过pundit进行授权并通过Devise进行身份验证时,我得到了这个错误。

代码语言:javascript
复制
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是

代码语言:javascript
复制
class ApplicationController < ActionController::Base

  include Pundit

  protect_from_forgery with: :exception

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

有一个application_policy.rb,如下所示

代码语言:javascript
复制
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

代码语言:javascript
复制
class TripPolicy < ApplicationPolicy
    def new?
    user.provider?
  end

  def create?
    user.provider?
  end

    def update?
    user.provider?
  end

end

在我的trips_controller中,我这样做

代码语言:javascript
复制
class TripsController < ApplicationController
    before_action :authenticate_user!
  after_action :verify_authorized, except: [:index, :new]

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

  ...

end

任何指针为什么我得到这个错误。已经伤了我的头一段时间了。

EN

回答 1

Stack Overflow用户

发布于 2016-04-16 11:50:45

弄清楚了问题所在。这是我的代码的一个问题。在authorize函数中,它引用接受参数的Policy函数。

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

有时,在问题上过夜,第二天醒来时有一个新的开始,是值得的。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36637999

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档