我有这样的表格:
<%= form_for(:quiz_responses, url: quiz_responses_path) do |f| %>
<%= f.hidden_field :name, value: @survey.name %>
<%= fields_for :questions do |ff| %>
<% @questions.each do |question| %>
<ul>
<%= ff.hidden_field "#{question_counter}", value: question.content %>
<%= ff.label question.content %>
<%= fields_for :answers do |fff| %>
<% question.answers.each do |answer| %>
<%= fff.hidden_field "#{answer_counter}", value: answer.content %>
<% answer_counter += 1 %>
<%= fff.label answer.content %>
<%= f.radio_button("user_answer[#{user_answer_counter}]", answer.content) %>
<% end #questions.answers.each do %>
<% end #fields_for answers %>
<% user_answer_counter += 1 %>
</ul>
<% question_counter += 1 %>
<% end #@questions.each do %>
<% end #fields_for questions %>
<%= f.submit %>
<% end #form_for%>
我想要的结果是我得到一个params哈希:quiz_responses包含:问题哈希,并且该哈希中的每个问题值包含:答案哈希包含答案。但这就是我所看到的:
其中包含所有问题的问题哈希和包含所有答案的单独答案哈希,以及在quiz_responses中的某种方式user_answeris。我该如何解决这个烂摊子?
答案 0 :(得分:2)
您需要在表单对象上调用fields_for
,如下所示:
<%= form_for(:quiz_responses, url: quiz_responses_path) do |f| %>
# ...
<%= f.fields_for :questions do |ff| %>
# ...
<%= ff.fields_for :answers do |fff| %>
# ...