我有两个模特。
Question.rb
Class Question
belongs_to :quiz
has_many :possible_answers
end
PossibleAnswer.rb
Class possible_answer
belongs_to :question
end
我试图通过对问题控制器和表单进行这些更改来为问题添加多个可能的答案。
questions_controller.rb
def new
@question = @quiz.questions.build
5.times { @question.possible_answers.build }
end
_form.html.erb
<p>
<label>Specify some choices:</label>
</p>
<%= f.fields_for :possible_answers do |c| %>
<p>
<%= c.text_field :title, placeholder: "Type your choice", class: "form-control" %>
</p>
<% end %>
根据我的阅读,它应该给5个字段输入可能的答案,但仍然给出单个字段。有人可以帮帮我吗?
答案 0 :(得分:0)
我认为你必须这样做:
def new
@question = @quiz.questions.build
5.times { @question.possible_answers << Possible_answer.build }
end