我需要为表单创建一些类型的帮助器,它将为一周中的每一天创建一个选择。
下面,创建5个实例,但只提交最后一个实例。
def basic_question(q)
a = ""
5.times do
a << select("question[question_id_#{q.id}]", :response, (0..30).to_a) + " for #{q.survey.publish_on.strftime('%A')} #{q.survey.publish_on.strftime('%D')} <br />"
end
return a.html_safe
end
以下是进行调查的观点
<%= form_for store_survey_path(@survey) do |f| %>
<% hidden_field.user_id, :value => current_user.id %>
<% @survey.questions.each do |q| %>
<li><%= q.content %></li>
<%= question_helper(q) %>
<% end %>
<p><%= f.submit %></p>
<% end %>
帮助者检查问题的类型。
def question_helper(question)
case question.question_type
when 'basic'
return basic_question(question)
when 'fill_in'
return fill_in_question(question)
when 'scale_5'
return "should return a scale of 5"
when 'scale_10'
return "should return a scale of 10"
when 'must_choose_answer'
return question.answers.to_s
when 'just_label'
return " I will be a label"
else
return "Couldn't find in helper"
end
end
答案 0 :(得分:0)
您可以选择单独选择不同的名称,以便单独提交,也可以将它们全部作为数组提交。我会选择最后一个选项。
要将它们作为数组提交,您必须将[]
添加到选择名称的末尾,如下所示:
question[question_id_#{q.id}][]
最终看起来像这样:
a << select("question[question_id_#{q.id}][]", :response, (0..30).to_a) + " for #{q.survey.publish_on.strftime('%A')} #{q.survey.publish_on.strftime('%D')} <br />"
我希望有所帮助。