首先,我有一个名为Answers
的模型用于我的琐事游戏。它存储了每个琐事问题的多个可能答案(测验has_many
答案)。我通过脚手架创建了一个表单,以便通过一组四个答案提交一个简单的UI来提交问题。
我想从一种形式做到这一点。当用户当前点击提交时,我可以发布所有四个答案 - 每个答案都有不同的answer_id
但共享相同的question_id
(因此我可以将4个答案与一个问题相关联) - 成功,就像这样:
<%= form_for(@question) do |f| %>
<% if @question.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@question.errors.count, "error") %> prohibited this question from being saved:</h2>
<ul>
<% @question.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="form-group">
<div class="field">
<%= f.label :question_text %><br>
<%= f.text_field :question_text, class: "form-control" %>
</div>
</div>
<div class="form-group">
<div class="field">
<%= f.label :category_id %><br>
<%= f.number_field :category_id, class: "form-control" %>
</div>
</div>
<h2>Answer Options</h2>
<%= f.fields_for :answers do |answer| %>
<div class="form-group">
<div class="answers">
<div class="field">
<%= answer.label :answer_1 %><br>
<%= answer.text_field :answer_text, class: "form-control" %>
</div>
</div>
</div>
<% end %>
<%= f.fields_for :answers do |answer| %>
<div class="form-group">
<div class="answers">
<div class="field">
<%= answer.label :answer_2 %><br>
<%= answer.text_field :answer_text, class: "form-control" %>
</div>
</div>
</div>
<% end %>
<%= f.fields_for :answers do |answer| %>
<div class="form-group">
<div class="answers">
<div class="field">
<%= answer.label :answer_3 %><br>
<%= answer.text_field :answer_text, class: "form-control" %>
</div>
</div>
</div>
<% end %>
<%= f.fields_for :answers do |answer| %>
<div class="form-group">
<div class="answers">
<div class="field">
<%= answer.label :answer_4 %><br>
<%= answer.text_field :answer_text, class: "form-control" %>
</div>
</div>
</div>
<% end %>
如果我检查控制台,它可以工作!我有四个不同的答案(因此它们在表格中有四行但具有相同的question_id
。奇怪的是,如果我然后使用编辑路线,而不是填充四个输入(如new
那样) ,它显示答案的16个text_field
输入(标记为答案1,答案2,答案3,答案4,答案1)。
最后,我认为可能存在问题的另一个原因是我如何通过控制器更新嵌套的Answers
属性,如下所示:
def question_params
params.require(:question).permit(:question_text, :category_id, :correct_answer, :answers_attributes => [:id, :answer_text])
end
我是否错误地设置了它,以便在编辑文件中显示时会迭代4倍?
答案 0 :(得分:1)
我认为在为edit
操作呈现表单时,您需要做些不同的事情:
您可以尝试这样的事情:
<%= form_for(@question) do |f| %>
<% if @question.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@question.errors.count, "error") %> prohibited this question from being saved:</h2>
<ul>
<% @question.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="form-group">
<div class="field">
<%= f.label :title %><br>
<%= f.text_field :title, class: "form-control" %>
</div>
</div>
<% unless @question.new_record? %>
<% @question.answers.each_with_index do |ans,i| %>
<%= f.fields_for :answers, ans do |answer| %>
<%= answer.label "answer_#{i+1}" %><br>
<%= answer.text_field :answer_text, class: "form-control" %>
<% end %>
<% end %>
<% else %>
<h2>Answer Options</h2>
<%= f.fields_for :answers, Answer.new do |answer| %>
<%= answer.label :answer_1 %><br>
<%= answer.text_field :answer_text, class: "form-control" %>
<% end %>
<%= f.fields_for :answers,Answer.new do |answer| %>
<%= answer.label :answer_2 %><br>
<%= answer.text_field :answer_text, class: "form-control" %>
<% end %>
<%= f.fields_for :answers,Answer.new do |answer| %>
<%= answer.label :answer_3 %><br>
<%= answer.text_field :answer_text, class: "form-control" %>
<% end %>
<%= f.fields_for :answers,Answer.new do |answer| %>
<%= answer.label :answer_4 %><br>
<%= answer.text_field :answer_text, class: "form-control" %>
<% end %>
<% end %>
<%= f.submit @question.new_record? ? "Add" : "Update" %>
<% end %>