R.lOOp
R.lOOp

Reputation: 15

Ruby on Rails -- Nested radio buttons issue (When one is selected other is deselected)

So I have a form in which each question has different options and one of the options in each question needs to be selected:

<% @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 %>

Suppose I select an option from one question( radio button ), when I move on to the next question and select another option the option I selected from the previous question becomes unselected. Can anyone tell em how to fix that?

Upvotes: 1

Views: 262

Answers (1)

SRack
SRack

Reputation: 12203

Your issue is using the same name for each set of radio buttons (exam_answers[][answer_mcq]).

If you update your code to use each_with_index or the question's ID (or whatever you like really) and add this on to the radio_button_tag name, this will work for you.

For example:

<% @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 %>

Or:

<%= 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}]"%>

Using the same name groups all the options together, meaning only one can be selected per group. Updating to use an approach as above will overcome this by differentiating the groups.

Hope that helps - let me know how you get on or if you have any questions.

Upvotes: 1

Related Questions