Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8923361
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T07:05:46+00:00 2026-06-15T07:05:46+00:00

I am trying to make a form for a student to take an exam

  • 0

I am trying to make a form for a student to take an exam (with multiple-choice questions).

Here are the relevant models:

Exam has_many :questions
     has_one :grade
     accepts_nested_attributes_for :questions
Question has_many :answers
     accepts_nested_attributes_for :answers

Grade belongs_to :exam
      belongs_to :subscription

I’m using Simple_form for my application’s forms. The form for making an exam is done and working using fields_for and some javascript (as shown in the railscast on nested forms). Now I’m trying to make the form for taking an exam.

The only inputs will be a set of radio buttons for each question. The user input will be used to calculate the score and the score will be the only information saved to the Grade model. I must admit I’m at a loss at how to make this work. Here’s what I have so far for the Grade#new view:

<%= simple_form_for [@subscription, @grade], :html => { :class => 'form-horizontal' } do |f| %>
  <fieldset>
    <legend>Exam for <%= @lesson.name.capitalize %></legend>

    <% @questions.each do |question| %>
      <%= simple_fields_for :questions do |builder| %>
        <ol>
          <li>
            <p><%= question.text %></p>
            <%= f.input :answer, :collection => question.answers, :as => :radio_buttons %>
          </li>
        </ol>
      <% end %>
    <% end %>

    <div class="form-actions">
      <%= f.submit "Submit Exam", :class => "btn btn-primary" %>
      <%= link_to "Cancel", lesson_exam_path(@lesson, @exam), :class=>"btn" %>
    </div>
  </fieldset>
<% end %>

This produces a form that looks correct (well, mostly anyways), but the radio buttons are all part of one set. When selecting an answer on one question, it deselects the answer from all other questions, since they’re part of the same set. Here’s the pertinent HTML generated:

<input class="radio_buttons optional" id="grade_answer_31" name="grade[answer]" type="radio" value="31"/>

The problem here of course is with the name= html attribute. It is pointing to grade. I need it to be something like name="grade[:question_id][:answer_id]". If I had that, I could collect the information easily in the controller and make the necessary calculations.

I’m making the view on the grades_controller since a grade is what will ultimately be created, but questions and answers are nested under the exam model which makes me a bit confused about where this form/action should reside. Should I maybe make a take_exam action on the exams_controller and handle creating the grade from there?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T07:05:48+00:00Added an answer on June 15, 2026 at 7:05 am

    Ok, so my problem was two-fold:

    1.) on my radio_buttons input, I mistakenly used the f variable from form_for instead of builder from the fields_for call
    2.) I changed fields_for like so:

    <%= simple_fields_for "questions[#{question.id}]" do |builder| %>
    

    Calling fields_for like that make a hash of all the radio_button inputs in the format grade[questions][:question_id][:answer_id] like I wanted.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to make a dynamic form of questions and answers, like so: Question
I'm trying to make a form that will take given text and direct you
I'm struggling myself trying to make my form work with my models... So, I
I am trying to make a form using two drop down select menus: one
I am trying to make a form field for a datetime property that allows
In a Symfony2 website I'm trying to make a form with 2 (or 3)
I'm fairly new to javascript and I'm trying to make an form for my
I'm trying to make a simple form, but it's working not so fine. This
I am trying to make an input form have a background image, but as
I'm trying to make an AJAX form that will update a twitter status when

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.