Rails嵌套表单错误:param丢失或值为空

时间:2015-02-24 06:33:01

标签: ruby-on-rails

我正在构建一个允许用户创建比赛的应用。每场比赛都有很多问题,每场比赛都有很多参赛作品。每个条目都有很多答案,每个问题都有很多答案。这是我的模特:

class Answer < ActiveRecord::Base
  belongs_to :entry
  belongs_to :question
end

class Contest < ActiveRecord::Base
  has_many :entries
  has_many :questions
end

class Entry < ActiveRecord::Base
  belongs_to :contest
  has_many :answers
  accepts_nested_attributes_for :answers, allow_destroy: true
end

class Question < ActiveRecord::Base
  has_many :answers
  belongs_to :contest
end

除了我尝试创建条目时,一切正常。我得到一个“参数丢失或值为空:条目”错误。这是我的控制器:

class EntriesController < ApplicationController
  before_action :set_entry, only: [:show, :edit, :update, :destroy]
  before_action :set_contest

  # GET /entries
  # GET /entries.json
  def index
    @entries = Entry.all
  end

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

  # GET /entries/new
  def new
    @entry = Entry.new
  end

  # GET /entries/1/edit
  def edit
  end

  # POST /entries
  # POST /entries.json
  def create
    @entry = Entry.new(entry_params)
    @entry.contest = @contest
    respond_to do |format|
      if @entry.save
        format.html { redirect_to @entry, notice: 'Entry was successfully created.' }
        format.json { render :show, status: :created, location: @entry }
      else
        format.html { render :new }
        format.json { render json: @entry.errors, status: :unprocessable_entity }
      end
    end
  end

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

  # DELETE /entries/1
  # DELETE /entries/1.json
  def destroy
    @entry.destroy
    respond_to do |format|
      format.html { redirect_to entries_url, notice: 'Entry was successfully destroyed.' }
      format.json { head :no_content }
    end
  end

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

    def set_contest
      @contest = Contest.find(params[:contest_id])
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def entry_params
      params.require(:entry).permit(:contest_id, answers_attributes: [:id,  :content, :entry_id, :question_id, :_destroy])
    end
end

这是我的报名表:

<%= simple_form_for([@contest, @entry]) do |f| %>
  <%= f.error_notification %>

  <div class="form-inputs">
    <h3>Questions</h3>
    <%= simple_fields_for :answers do |ff| %>
        <% @contest.questions.each do |question| %>
            <h4><%= question.content %></h4>
            <%= ff.input :content, input_html: {class: 'form-control'} %>
        <% end %>
    <% end %>
  </div>

  <div class="form-actions">
    <%= f.button :submit %>
  </div>
<% end %>

我仍然在研究逻辑,但我很困惑为什么报名表格给了我这个错误。任何帮助将不胜感激!

更新

在Rails指南示例中,它们将新操作显示为:

def new
  @person = Person.new
  2.times { @person.addresses.build}
end

我是否需要在新动作中构建答案对象?我不确定......我试过但它没用。我觉得这不是问题,因为错误来自entry_params方法

1 个答案:

答案 0 :(得分:0)

您应该将此行添加到 new 操作中。

@entry.answers.build

并更改此行

<%= simple_fields_for :answers do |ff| %>

<%= f.simple_fields_for :answers do |ff| %>