Rails / Pundit ArgumentError(错误的参数个数(2个为0))

时间:2014-08-23 03:24:57

标签: ruby-on-rails-4 devise pundit

我一直在反对这一天。我正在尝试为名为Design的模型实现一个权威策略(使用Devise进行身份验证),该模型属于具有许多设计的用户。还应该创建和新的行动后授权除外?看起来这应该有效。非常感谢

我一直遇到

  

ArgumentError(参数数量错误(0表示0)):

创建新设计时('调试器')。我认为它正在将有效的@design传递给政策发现者。这可能是我在策略中设置范围的方式。

这是设计控制器:

class DesignsController < ApplicationController
  before_filter :authenticate_user!
  before_action :set_design, only: [:show, :edit, :update, :destroy]
  after_action :verify_authorized, except: [:index, :new]

  # GET /designs
  # GET /designs.json
  def index
    @designs = policy_scope(Design)

  end

  # GET /designs/1
  # GET /designs/1.json
  def show
  end

  # GET /designs/new
  def new
    @design = Design.new  

  end

  # GET /designs/1/edit
  def edit
  end

  # POST /designs
  # POST /designs.json
  def create
    @design = Design.new(design_params)
     @design.user_id = current_user.id

    respond_to do |format|
      if @design.save
        format.html { redirect_to @design, notice: 'Design was successfully created.' }
        format.json { render :show, status: :created, location: @design }
      else
        format.html { render :new }
        format.json { render json: @design.errors, status: :unprocessable_entity }
      end
    end
    debugger // This is where it throws the exception
    authorize @design
  end

  # PATCH/PUT /designs/1
  # PATCH/PUT /designs/1.json
  def update
    respond_to do |format|
      if @design.update(design_params)
        format.html { redirect_to @design, notice: 'Design was successfully updated.' }
        format.json { render :show, status: :ok, location: @design }
      else
        format.html { render :edit }
        format.json { render json: @design.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /designs/1
  # DELETE /designs/1.json
  def destroy
    @design.destroy
    respond_to do |format|
      format.html { redirect_to designs_url, notice: 'Design was successfully destroyed.' }
      format.json { head :no_content }
    end
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_design
      @design = Design.find(params[:id])
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def design_params
      params.require(:design).permit(:name, :description, :design_model, :certification_for_distribution, :manufacturing_test)
    end
end

以下是设计政策类

class DesignPolicy 

  class Scope<DesignPolicy
    attr_reader :user, :scope

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

    def resolve
      if user.admin?

        @scope.all
      else

        @scope.all.where(user_id: user.id)
      end
    end
  end

  def index?
    debugger
    true
  end

  def new?
    debugger
    @current_user != nil
  end

  def create?
    @current_user != nil
    new?
  end



end

1 个答案:

答案 0 :(得分:1)

  1. 范围不应继承自DesignPolicy https://github.com/elabs/pundit#scopes
  2. 您需要将DesignPolicy设置为具有初始化方法https://github.com/elabs/pundit#policies
  3. 如果他们从ApplicationPolicy继承,那么您不需要在每个策略中设置初始化方法。专家文档非常好,我几乎每天都参考它。只要您了解Ruby中的类和继承,就应该没有问题。