Thursday, 8 August 2013

Rails nested attributes in forms - forms collapsed into one

Rails nested attributes in forms - forms collapsed into one

So I have a site of quizzes. Users go on and take a quiz. The way I have
this setup is with three main models: quizzes, questions, answers.
Question model instances are created by admins, ie me. When a user takes a
new quiz, the quiz randomly selects some of these questions. Their
responses are recorded in the answers models. So every quiz might have 5
questions, and 5 answers. Questions aren't changed by users, but answers
are. A question has_many answers, and answers have a question_id field.
Below is my code. My problem is that the partial I render, which contains
the questions' (multiple choice questions) possible answers, which are
radio buttons. However, all the radio buttons, even though they belong to
separate question/answer models, are being treated together. When I select
one radio button in one question, then a radio button in another
question/answer is deselected. Obviously, this shouldn't happen. Help?
Main quiz html:
<div class="square span18 offset3" id="take-quiz-portal"
style="display:none;">
<div class="row">
<div class="span17">
<h1 id="quiz-heading">You are now taking a <%= quiz.category%>
quiz! </h1>
<hr/>
</div>
</div>
<%= simple_form_for quiz, :url => {:controller => "quizzes", :action =>
'check_quiz_answers'}, :remote => true do |f| %>
<div class="row">
<div class="span18" id="questions-paragraph">
<%= f.hidden_field :is_completed, :value => true %>
<% debug quiz %>
<% quiz.mc_questions.each do |mc_question| %>
<% answer = mc_question.get_answer(quiz.id) %>
<%= render partial: "multiple_choice_questions/show_mc_question",
locals: {user: user, mc_question: mc_question, possible_answers:
shuffle_answers(mc_question), f: f, quiz_id: quiz.id, answer:
answer } %>
<% end %>
<% quiz.fr_questions.each do |free_response_question| %>
<%= render partial: "free_response_questions/show_question",
locals: {user: user, free_response_question:
free_response_question, f: f } %>
<% end %>
</div>
</div>
<div class="row" id="quiz-completed-btn" style="margin-top: 20px">
<div class="t-center">
<%= f.submit "I am done!", data: { confirm: 'Are you sure you are
done?'}, class: "btn btn-large btn-danger take-quiz-form-btn",
style: "width:200px"%>
</div>
</div>
<% end %>
<div class="row" id="start-new-quiz-btn" style="margin-top:10px;
display:none">
<div class="t-center">
<%= link_to "I am done!", nil, class: "btn btn-large btn-danger",
style: "color:white; width:200px"%>
</div>
</div>
Now the partial that is rendered:
<%= f.fields_for answer do |mc| %>
<%= mc.hidden_field :is_correct, :value => true %>
<div class="row">
<div class="span16">
<b><%= mc_question.title %></b>
</div>
</div>
<div class="row">
<div class="span16">
<%= mc_question.prompt %>
</div>
</div>
<div class="row">
<div class="span16">
<%= mc.input_field :user_response, collection: possible_answers,
:item_wrapper_class => "inline take-quiz-form", as: :radio_buttons,
wrapper_html: {class: 'take-quiz-form'} %>
</div>
</div>
<% end %>
Many thanks!

No comments:

Post a Comment