Ruby on rails 为什么我所有的单选按钮都共享一个选项?

Ruby on rails 为什么我所有的单选按钮都共享一个选项?,ruby-on-rails,Ruby On Rails,我正试图建立一个调查应用程序,但我遇到了一个障碍。当我提交对调查的回复时,我的所有答案共享相同的单选按钮选择id,这意味着我只能在多个问题中选择一个单选按钮,而我希望每个问题都有一个单选按钮 我很困惑,非常感谢您的帮助 我正在为这个项目使用简单的表单 视图/响应/_form.html.erb 模型 survey.rb 问题.rb 答案.rb response.rb 谢谢, 巴姆斯 编辑: 生成的HTML 为q.answers中的每个元素生成一个名为response[answer\u id]的无线

我正试图建立一个调查应用程序,但我遇到了一个障碍。当我提交对调查的回复时,我的所有答案共享相同的单选按钮选择id,这意味着我只能在多个问题中选择一个单选按钮,而我希望每个问题都有一个单选按钮

我很困惑,非常感谢您的帮助

我正在为这个项目使用简单的表单

视图/响应/_form.html.erb 模型 survey.rb 问题.rb 答案.rb response.rb 谢谢, 巴姆斯

编辑:

生成的HTML 为q.answers中的每个元素生成一个名为response[answer\u id]的无线电元素。 radio元素的name属性将它们定义为属于表单中的同一组

在生成的代码中,所有无线电输入都有name=response[answer_id],因此它们都是互斥的

每个问题需要有不同的名称值。如何做到这一点是你的选择

为q.answers中的每个元素生成一个名为response[answer\u id]的无线电元素。 radio元素的name属性将它们定义为属于表单中的同一组

在生成的代码中,所有无线电输入都有name=response[answer_id],因此它们都是互斥的


每个问题需要有不同的名称值。您可以选择如何执行此操作。

您可以为有问题的单选按钮添加生成的html吗?@Sokyoku:好的,添加了。您可以为有问题的单选按钮添加生成的html吗?@Sokyoku:好的,添加了。此关联用于响应。答案。变量“q”只是用于迭代调查中存在的问题。因此,执行f.association q.name不起作用。只是想澄清变量“q”来自@survey.questions。该关联用于response.answers。变量“q”只是用于迭代调查中存在的问题。因此,执行f.association q.name不起作用。只是想澄清变量“q”来自@survey.questions。
<%= @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
<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="&#x2713;" /><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>
f.association :answers, collection: q.answers, as: :radio_buttons