ruby on rails 4 simple_form simple_fields_for 未在嵌套模型中呈现 html 代码

ruby on rails 4 simple_form simple_fields_for not rendering html code in nested model

我在为嵌套模型正确呈现表单时遇到了一些问题。基本上,我有 2 个模型,一个 Question 模型和一个 Answer 模型。 Question 模型 has_many AnswersAnswer belongs_toQuestion。因此,根据文档 here and here,我设置了我的模型、控制器和表单,如下所示:

Question 型号:

class Question < ActiveRecord::Base
    has_many :answers, :dependent => :destroy
    accepts_nested_attributes_for :answers, :reject_if => lambda {|attrib| attrib[:content].blank? }, :allow_destroy => true
    validate :validate_answers

    def validate_answers
        remaining_answers = answers.reject(&:marked_for_destruction?)
        errors.add :answers, "At least 4 answers are required." if remaining_answers < 4
    end
end

Answer 型号:

class Answer < ActiveRecord::Base
    belongs_to :question
end

questions_controller.rb

class QuestionsController < ApplicationController
  before_action :set_question, only: [:show, :edit, :update, :destroy]

  # GET /questions
  # GET /questions.json
  def index
    @questions = Question.all
  end

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

  # GET /questions/new
  def new
    @question = Question.new

    4.times { @question.answers.build }

  end

  # GET /questions/1/edit
  def edit
  end

  # POST /questions
  # POST /questions.json
  def create
    @question = Question.new(question_params)

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

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

  # DELETE /questions/1
  # DELETE /questions/1.json
  def destroy
    @question.destroy
    respond_to do |format|
      format.html { redirect_to questions_url, notice: 'Question was successfully destroyed.' }
      format.json { head :no_content }
    end
  end

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

    # Never trust parameters from the scary internet, only allow the white list through.
    def question_params
      params.require(:question).permit()
    end
end

app/views/questions/_form.html.erb:

<%= simple_form_for @question do |f| %>
  <%= f.error_notification %>

  <div class="form-inputs">
    <p>
        <%= f.label :question_stem, "Question" %>
        <%= f.text_area :question_stem, :rows => 3 %>
    </p>

    <p>
    <% f.simple_fields_for :answers do |builder| %>
        <%= builder.label :answer_text, "Answer" %><br>
        <%= builder.text_area :answer_text %>   
    <% end %>
    </p>
  </div>


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

但是,当我将浏览器指向 http://localhost:3000/questions/new 这是我得到的:

知道发生了什么事吗? rails 控制台显示已收到请求并已呈现表单:

Started GET "/questions/new" for 127.0.0.1 at 2015-08-27 01:03:57 -0500
Processing by QuestionsController#new as HTML
  Rendered questions/_form.html.erb (4.9ms)
  Rendered questions/new.html.erb within layouts/application (17.3ms)
Completed 200 OK in 221ms (Views: 179.1ms | ActiveRecord: 1.2ms)

我很困惑为什么没有呈现答案模型的字段。非常感谢任何帮助!

问题是因为你在这一行 <% f.simple_fields_for :answers do |builder| %>

中缺少 =

应该是

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