我正在尝试建立一个调查应用程序,但我遇到了障碍。当我提交对调查的回复时,我的所有答案都共享相同的单选按钮选择ID,这意味着我可以仅在多个问题中选择一个单选按钮,而我希望这是一个单选按钮 per 问题。
我很困惑,非常感谢你的帮助。
我正在为这个项目使用simple_form。
<%= @response %>
<%= @response.survey_id %>
<%= @survey.name %>
<p>
<strong>Name:</strong>
<%= @survey.name %>
</p>
<p>
<strong>End date:</strong>
<%= @survey.end_date %>
</p>
<p>
<%= simple_form_for [@survey, @response] do |f| %>
<!-- Survey id -->
<%= f.hidden_field :survey_id, :value => @survey.id %>
<!-- Questions -->
<ol>
<% @survey.questions.each do |q| %>
<li><%= q.content %></li>
<!-- Iterate through answers for this question -->
<%= f.simple_fields_for :answers do |a| %>
<%= f.association :answers, collection: q.answers, as: :radio_buttons, :item_wrapper_class => 'test_class', include_blank: false %>
<% end %>
<% end %>
</ol>
<%= f.submit %>
<% end %>
</p>
class Survey < ApplicationRecord
has_many :questions, :dependent => :destroy
has_and_belongs_to_many :responses
validates_presence_of :name
accepts_nested_attributes_for :questions, allow_destroy: true
end
class Question < ApplicationRecord
belongs_to :survey, optional: true
has_many :answers, :dependent => :destroy
accepts_nested_attributes_for :answers, allow_destroy: true
end
class Answer < ApplicationRecord
belongs_to :question, optional: true
belongs_to :response, optional: true
def to_label
"#{content}"
end
end
class Response < ApplicationRecord
belongs_to :survey, optional: true
belongs_to :user, optional: true
has_and_belongs_to_many :answers, :dependent => :destroy
end
谢谢, Bhams
编辑:
<p>
<form novalidate="novalidate" class="simple_form new_response" id="new_response" action="/surveys/9/responses" accept-charset="UTF-8" method="post"><input name="utf8" type="hidden" value="✓" /><input type="hidden" name="authenticity_token" value="39QorGeTDhu7pojy3L6gbZ1GZh9yBhqZphFMRWD++Ghd2ScWeLVEcdyhD9Ym0JyKHLptghvLOnJP9UlmHn32Bw==" />
<!-- Survey id -->
<input value="9" type="hidden" name="response[survey_id]" id="response_survey_id" />
<!-- Questions -->
<ol>
<li>222</li>
<!-- Iterate through answers for this question -->
<div class="input radio_buttons optional response_answers"><label class="radio_buttons optional">Answers</label><input type="hidden" name="response[answer_ids]" value="" /><span class="radio test_class"><label for="response_answer_ids_35"><input class="radio_buttons optional" type="radio" value="35" name="response[answer_ids]" id="response_answer_ids_35" />11111</label></span><span class="radio test_class"><label for="response_answer_ids_36"><input class="radio_buttons optional" type="radio" value="36" name="response[answer_ids]" id="response_answer_ids_36" />2222</label></span><span class="radio test_class"><label for="response_answer_ids_37"><input class="radio_buttons optional" type="radio" value="37" name="response[answer_ids]" id="response_answer_ids_37" />33333</label></span><span class="radio test_class"><label for="response_answer_ids_38"><input class="radio_buttons optional" type="radio" value="38" name="response[answer_ids]" id="response_answer_ids_38" />444444</label></span><span class="radio test_class"><label for="response_answer_ids_39"><input class="radio_buttons optional" type="radio" value="39" name="response[answer_ids]" id="response_answer_ids_39" />55555</label></span></div>
<li>33333333333</li>
<!-- Iterate through answers for this question -->
<div class="input radio_buttons optional response_answers"><label class="radio_buttons optional">Answers</label><input type="hidden" name="response[answer_ids]" value="" /><span class="radio test_class"><label for="response_answer_ids_40"><input class="radio_buttons optional" type="radio" value="40" name="response[answer_ids]" id="response_answer_ids_40" />4444444444444</label></span><span class="radio test_class"><label for="response_answer_ids_41"><input class="radio_buttons optional" type="radio" value="41" name="response[answer_ids]" id="response_answer_ids_41" />55555</label></span></div>
</ol>
<input type="submit" name="commit" value="Create Response" data-disable-with="Create Response" />
</form></p>
答案 0 :(得分:0)
f.association :answers, collection: q.answers, as: :radio_buttons
为q.answers中的每个元素生成一个名为“response [answer_ids]”的无线电元素。 无线电元素的name属性将它们定义为属于表单内的同一组。
在生成的代码中,您的所有无线电输入均为name="response[answer_ids]"
,因此它们都是互斥的。
每个问题都需要有不同的name
值。你如何做到这一点是你的选择。