ActionController :: ParameterMissing不知道什么是错的

时间:2014-04-21 17:43:17

标签: ruby-on-rails ruby parameters

我一直收到这个错误,我暂时没有使用红宝石,所以我真的不知道如何修复它。

param is missing or the value is empty: sch

Extracted source (around line #72):
70
71
72
73
74

    # Never trust parameters from the scary internet, only allow the white list through.
    def sch_params
      params.require(:sch).permit(:date, :time, :user_id)
    end
end

Rails.root: C:/Sites/web

Application Trace | Framework Trace | Full Trace
app/controllers/sches_controller.rb:72:in `sch_params'
app/controllers/sches_controller.rb:27:in `create'
Request

Parameters:

{"utf8"=>"✓",
 "authenticity_token"=>"cHq54V7aVCjeBz0udZAYjGiojgrqDANx/BS+oMQ+N+0=",
 "sche"=>{"date(1i)"=>"2014",
 "date(2i)"=>"4",
 "date(3i)"=>"21",
 "time(1i)"=>"2014",
 "time(2i)"=>"4",
 "time(3i)"=>"21",
 "time(4i)"=>"17",
 "time(5i)"=>"37",
 "user"=>"steve"},
 "commit"=>"Create Sche"}

这是我的控制器

class SchesController < ApplicationController
  before_action :set_sch, only: [:show, :edit, :update, :destroy]

  # GET /sches
  # GET /sches.json
  def index
    @sches = Sche.all
  end

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

  # GET /sches/new
  def new
    @sch = Sche.new
  end

  # GET /sches/1/edit
  def edit
  end

  # POST /sches
  # POST /sches.json
  def create
    @sch = Sche.new(sch_params)

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

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

  # DELETE /sches/1
  # DELETE /sches/1.json
  def destroy
    @sch.destroy
    respond_to do |format|
      format.html { redirect_to sches_url }
      format.json { head :no_content }
    end
  end

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

    # Never trust parameters from the scary internet, only allow the white list through.
    def sch_params
      params.require(:sch).permit(:date, :time, :user)
    end
end

我真的不知道发生了什么事,我已经尝试了解问题,但我仍然没有找到答案 任何帮助将不胜感激,谢谢

1 个答案:

答案 0 :(得分:0)

使用此:

   def sch_params
      params.require(:sche).permit(:date, :time, :user)
    end

您的控制器名称为SchesController,因此在params hash中,您获得的是密钥sche,而不是sch

如果查看生成的参数,请注意sche

  

&#34; sche&#34; =&gt; {&#34; date(1i)&#34; =&gt;&#34; 2014&#34;,&#34; date(2i)&#34; =&gt;&#34; 4&#34;,&#34; date(3i)&#34; =&gt;&#34; 21&#34;,   &#34;时间(1i)&#34; =&gt;&#34; 2014&#34;,&#34;时间(2i)&#34; =&gt;&#34; 4&#34;,&# 34;时间的(3R)&#34; = GT;&#34; 21&#34 ;,   &#34;时间(4i)&#34; =&gt;&#34; 17&#34;,&#34;时间(5i)&#34; =&gt;&#34; 37&#34;,&# 34;用户&#34; = GT;&#34;史蒂夫&#34;}