我在为嵌套模型正确呈现表单方面遇到了一些麻烦。基本上,我有2个模型Question
模型和Answer
模型。 Question
模型has_many
Answers
和Answer
belongs_to
一个Question
。因此,根据文档here和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)
我很难过为什么答案模型的字段没有被渲染。非常感谢任何帮助!