【问题标题】:Ruby on Rails -- Nested radio buttons issue (When one is selected other is deselected)Ruby on Rails -- 嵌套单选按钮问题(当一个被选中时,另一个被取消选中)
【发布时间】:2018-12-18 12:50:35
【问题描述】:

所以我有一个表格,其中每个问题都有不同的选项,并且需要选择每个问题中的一个选项:

<% @exam.exam_questions.each do |question|%>
                            <div class="item">
                                <div class="individual_question_container">
                                    <div class="header">
                                      <h3>Question: <%= question.question_no %> / <%= @exam.exam_questions.count %></h3>
                                    </div>  
                                    <div class="container_body">
                                      <br />
                                      <h4><%= question.question%></h4>
                                      <br />
                                    </div>
                                    <div class="footer">
                                        <br />
                                        <% if question.question_type == 1%>
                                            <% if question.correct_answers.split(',').count == 1 %>
                                                <% question.options.split(',').each do |option| %>
                                                    <%= radio_button_tag :exam_answer_answer_mcq, option_position_to_answer(question.options.split(',').find_index(option)), false, :name => "exam_answers[][answer_mcq]"%> <!-- RADIO BUTTON TAG HERE -->
                                                    <label><%= option %></label>
                                                    <br />
                                                <% end %>
                                            <% else %>
                                                <% question.options.split(',').each do |option| %>
                                                    <input type="checkbox" name ="<%=question.id%>--<%=question.options.split(',').find_index(option)%>--name" value="<%=option_position_to_answer(question.options.split(',').find_index(option))%>" id="<%=question.id%>--<%=question.options.split(',').find_index(option)%>--id" onclick="add_to_textbox('<%=question.id%>--<%=question.options.split(',').find_index(option)%>--id','<%=question.id%>--mainTextBox')">
                                                    <label><%= option %></label>
                                                    <br />
                                                <% end %>
                                                <%= hidden_field_tag :exam_answer_answer_mcq, '', :name => "exam_answers[][answer_mcq]", id:"#{question.id}--mainTextBox"%>
                                            <% end %>
                                        <% elsif question.question_type == 2 %>
                                            <%= text_area_tag :exam_answer_answer_seq, '', :name => "exam_answers[][answer_seq]", :class => "form-control", :rows => 15, :placeholder => "Answer...."%>
                                        <% end %>
                                        <br />
                                    </div>
                                </div>
                            </div>
                            <br />
                            <br />
                        <%= hidden_field_tag :exam_answer_exam_question_id, question.id, :name => "exam_answers[][exam_question_id]"%>
                        <%= hidden_field_tag :exam_answer_student_id, current_student.id, :name => "exam_answers[][student_id]"%>
                        <%= hidden_field_tag :exam_answer_exam_id, question.exam_id, :name => "exam_answers[][exam_id]"%>
                        <% end %>

假设我从一个问题(单选按钮)中选择一个选项,当我转到下一个问题并选择另一个选项时,我从上一个问题中选择的选项变为未选中。谁能告诉他们如何解决这个问题?

【问题讨论】:

    标签: ruby-on-rails ruby forms nested radio-button


    【解决方案1】:

    您的问题是每组单选按钮使用相同的名称 (exam_answers[][answer_mcq])。

    如果您更新代码以使用 each_with_index 或问题的 ID(或您真正喜欢的任何内容)并将其添加到 radio_button_tag 名称中,这将适合您。

    例如:

    <% @exam.exam_questions.each_with_index do |question, i|%>
      ...
      <%= radio_button_tag :exam_answer_answer_mcq, option_position_to_answer(question.options.split(',').find_index(option)), false, :name => "exam_answers[][answer_mcq_#{i}]"%>
      ...
    <% end %>
    

    或者:

    <%= radio_button_tag "exam_answer_answer_mcq_#{i}", option_position_to_answer(question.options.split(',').find_index(option)), false, :name => "exam_answers[][answer_mcq_#{question.id}]"%>
    

    使用相同的名称将所有选项组合在一起,这意味着每个组只能选择一个。更新以使用上述方法将通过区分组来克服这个问题。

    希望对您有所帮助 - 如果您有任何问题,请告诉我。

    【讨论】:

    • 谢谢,这是有道理的,但是我该如何传递参数:` params[:exam_questions].each do |exam_question_params|参数 = ActionController::Parameters.new(exam_question_params)exam_question = ExamQuestion.new(parameters.permit(:question, :correct_answers, :question_no, :options, :question_type, :exam_id))exam_question.save end `
    • 我建议提出一个新问题——在评论中很难弄清楚:) 让单选按钮工作,然后添加一个问题,显示你想要的参数以及它们的方式'目前正在进来,回答会很好而且很清楚。那么好?如果您愿意,请随时在里面“@”我。希望这在这里有所帮助 - 如果是这样并且您认为它可以解决这个特定问题,请勾选问题以接受它作为答案。
    猜你喜欢
    • 2020-10-21
    • 1970-01-01
    • 2017-07-19
    • 1970-01-01
    • 2019-03-25
    • 2020-11-13
    • 2011-12-27
    • 2017-07-09
    • 2015-06-18
    相关资源
    最近更新 更多