Ruby-on-rails 参数缺失或值为空:ResultController#update 中的 ParameterMissing

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30825735/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 00:15:24  来源:igfitidea点击:

Param is missing or the value is empty: ParameterMissing in ResultsController#update

ruby-on-railsruby-on-rails-4

提问by user2926430

I have a Result that belongs to a Website. After I create the website I also create the result and redirect to its edit page. Here I want to add some more values.

我有一个属于网站的结果。创建网站后,我还创建了结果并重定向到其编辑页面。在这里我想添加一些更多的值。

My problem is: When I try to update my Result, then I get:

我的问题是:当我尝试更新我的结果时,我得到:

param is missing or the value is empty: result


    Request

    Parameters:

    {"utf8"=>"?",  "_method"=>"patch",  "authenticity_token"=>"GRN/y/04Qbsm9DzlUAbUYF8ZSv2EMHnRZgBZY/6GMDlOBdq8V5Uncij9VRp51uydC6M/qc61jPWwpUehSuc5xA==", "data"=>["//html/body/div[position() = 3]/ul/li[position() = 16]/ul/li[position() = 2]/child::text()",  "//html/body/div[position()
    = 3]/ul/li[position() = 16]/ul/li[position() = 2]/p/a/child::text()",  "//html/body/div[position() = 3]/ul/li[position() = 16]/ul/li[position() = 4]/child::text()",  "//html/body/div[position()
    = 3]/ul/li[position() = 16]/ul/li[position() = 5]/a/child::text()"],  "commit"=>"Update Result",  "id"=>"66"}

This is how my Result params looks like

这就是我的结果参数的样子

def result_params
      params.require(:result).permit(:data)
    end

My model:

我的型号:

class Result < ActiveRecord::Base
  belongs_to :website
  attr_accessor :website_id
  attr_accessor :data

  serialize :data, Array
end

Here is my controller code:

这是我的控制器代码:

    class ResultsController < ApplicationController
  before_action :set_result, only: [:show, :edit, :update, :destroy]

  # GET /Results
  # GET /Results.json
  def index
    @results = Result.all
  end

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

  # GET /Results/new
  def new
    @result = Result.new
  end

  # GET /Results/1/edit
  def edit
    @result = Result.find(params[:id])
  end

  # POST /Results
  # POST /Results.json
  def create
    @result = Result.new(result_params)

    respond_to do |format|
      if @result.save
        format.html { redirect_to @result, notice: 'Result was successfully created.' }
        format.json { render :show, status: :created, location: result }
      else
        format.html { render :new }
        format.json { render json: @result.errors, status: :unprocessable_entity }
      end
    end
  end

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

  # DELETE /Results/1
  # DELETE /Results/1.json
  def destroy
    @result.destroy
    respond_to do |format|
      format.html { redirect_to results_url, notice: 'Result was successfully destroyed.' }
      format.json { head :no_content }
    end
  end


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

    # Never trust parameters from the scary internet, only allow the white list through.
    def result_params
      params.permit(:data => [])
    end
end

My view:

我的看法:

<%= form_for(@result) do |f| %>
  <% if @result.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(@result.errors.count, "error") %> prohibited this result from being saved:</h2>

      <ul>
      <% @result.errors.full_messages.each do |message| %>
        <li><%= message %></li>
      <% end %>
      </ul>
    </div>
  <% end %>


    <div class="field">
      <% if @result.website.url != nil %>
      <%= atts = get_all_elements(@result.website.url)%>
          <% atts.each do |p| %>
              <div>
                <%= check_box_tag "data[]", get_xpath_from_node(p)%>
                <%= p.text %>
              </div>
          <%end%>
      <% end%>
    </div>

  <div class="actions">
    <%= f.submit %>
  </div>
<% end %>

And this is the place where i call the edit result page:

这是我调用编辑结果页面的地方:

def update
    respond_to do |format|
      if @website.update(website_params)
        format.html { redirect_to @website, notice: 'Website was successfully updated.' }
        format.json { render :show, status: :ok, location: @website }
      else
        format.html { render :edit }
        format.json { render json: @website.errors, status: :unprocessable_entity }
      end
    end
  end

Ive allready tryed every solution I could find, but none of them seemed to work for me.

我已经尝试了我能找到的所有解决方案,但似乎没有一个对我有用。

回答by Nitish Parkar

The problem lies here:

问题出在这里:

params.require(:result).permit(:data)

From requiredocumentation,

需要文档,

require ensures that a parameter is present. If it's present, returns the parameter at the given key, otherwise raises an ActionController::ParameterMissing error.

require 确保存在参数。如果存在,则返回给定键处的参数,否则引发 ActionController::ParameterMissing 错误。

You are requiring resultparameter but it's missing from the params. All your values are inside dataparam. Removing require should do the trick.

您需要result参数,但参数中缺少它。你所有的值都在data参数里面。删除 require 应该可以解决问题。

params.permit(:data)

If you want to keep require, wrap datainside resultin forms.

如果您想保留要求,请用表格包裹data在里面result

回答by TorvaldsDB

I know, what you met. if you strong params is

我知道,你遇到了什么。如果你强大的参数是

def result_params
  params.require(:result).permit(:data)
end

Your parameters should has the format like this

你的参数应该有这样的格式

Parameters: {"result"=>{"data"=>"string"}}

and your parameters is just

你的参数只是

Parameters: {"data"=>"string"}

so you should remove the "result"

所以你应该删除“结果”

回答by Hymantrades

This error also occurs if you try to have a form submit without any fields. If you just want the single value you're passing through a button param you can use a hidden_field as a work around.

如果您尝试提交没有任何字段的表单,也会发生此错误。如果您只想要通过按钮参数传递的单个值,您可以使用 hidden_​​field 作为解决方法。