我创建了三个模型,“调查”,“问题”和“答案”。调查has_many问题has_many答案(他们都属于他们上面的模型)。
我在SurveyController编辑操作中使用此表单创建测验。在SurveyController的show
操作中,我想进行我创建的测验,并为用户生成测验本身。它提交给QuizResponses控制器的创建操作。
<%= form_for(:quiz_responses, url: quiz_responses_path) do |f| %>
<%= hidden_field_tag :name, @survey.name %>
<% @questions.each do |question| %>
<ul>
<%= hidden_field_tag :question, question.content %>
<%= f.label question.content %>
<% question.answers.each do |answer| %>
<%= f.label answer.content %>
<%= f.radio_button(:user_answer, answer.content) %>
<% end %>
</ul>
<% end #@questions.each do %>
<%= f.submit %>
<% end #form_for%>
答案 0 :(得分:1)
问题太广泛了,但这是一个简单的解决方案:
<%= form_for(:quiz_responses, url: quiz_responses_path) do |f| %>
<%= hidden_field_tag :name, @survey.name %>
<% @questions.each do |question| %>
<%= hidden_field_tag :question, question.content %>
<!-- question title -->
<h3><%= f.label question.content %></h3>
<!-- questions answers -->
<ul>
<% question.answers.each do |answer| %>
<!-- use display:inline-block or float to make inline li -->
<li>
<%= f.label answer.content %>
<%= f.radio_button(:user_answer, answer.content) %>
</li>
<% end %>
</ul>
<% end %>
<!-- submit -->
<p>
<%= f.submit %>
</p>
<% end %>