Question

I'm trying to make a rails quiz application and am having a problem. I'm using the nested_forms gem and I can normally create quizzes. Here is the code for creating quizzes:

_form.html.erb:

<%= nested_form_for @quiz do |f| %>

    <%= f.text_field :name %>

    <%= f.fields_for :questions do |question_form| %>

        <h5>Question</h5>

            <%= question_form.text_field :question %>

            <%= question_form.text_field :option1 %>

            <%= question_form.radio_button :trueoption, 1 %>

            <%= question_form.text_field :option2 %>

            <%= question_form.radio_button :trueoption, 2 %>

            <%= question_form.text_field :option3 %>

            <%= question_form.radio_button :trueoption, 3 %>

            <%= question_form.link_to_remove "Remove" %>
    <% end %>


        <%= f.link_to_add "Add a question", :questions %>

        <%= f.submit "I'm Done!", class: "button" %>

<% end %>

This works perfectly, the quiz is created and saved. But then when trying to solve the quiz the problems occur. When I go on a show view of a quiz (like quizzes/8) I'm showing the quiz normally with this:

            <%= nested_form_for @result do |f| %>

                    <% @quiz.questions.each do |question| %>
                        <%= f.fields_for question do |q| %>
                            <div class="panel">

                                <h2><%= question.question %></h2>
                                <h5><%= q.radio_button :trueoption, 1, id: question.id %><%= label :trueoption_option1, question.id %></h5>
                                <h5><%= q.radio_button :trueoption, 2, id: question.id %><%= label :trueoption_option2, question.id %></h5>
                                <h5><%= q.radio_button :trueoption, 3, id: question.id %><%= label :trueoption_option3, question.id %></h5>


                            </div>
                        <% end %>
                    <% end %>
            <%= f.submit %>
        <% end %>

And it shows each question normally, like it should. But what doesn't work are the radio buttons; it's like they are all the same, only 1 can be checked out of all. What is the problem?

Here are the controllers:

quizzes_controller.rb:

class QuizzesController < ApplicationController
def index
  @quizzes = Quiz.all
end

def new
  @quiz = Quiz.new
  3.times do
    question = @quiz.questions.build
  end
end

def create
  @quiz = Quiz.create(params[:quiz])
  if @quiz.save
    redirect_to @quiz
  else
    render :action => :new
  end
end

def show
  @quiz = Quiz.find(params[:id])
  @result = Result.new
end
end

Results controller:

class ResultsController < ApplicationController
def new
  @result = Result.new
end

def create
  @result = Result.create(params[:result])

  if @result.save
    redirect_to @result
  else
    render action: :new
  end
end

def show
  @result = Result.find(params[:id])
end
end

And the models:

Quiz.rb

class Quiz < ActiveRecord::Base
  attr_accessible :name, :questions_attributes

  has_many :questions

  accepts_nested_attributes_for :questions, allow_destroy: true
end

Question.rb

class Question < ActiveRecord::Base
  attr_accessible :option1, :option2, :option3, :trueoption, :quiz_id, :question

  belongs_to :quiz
  has_many :answers
end

Result.rb

class Result < ActiveRecord::Base
  attr_accessible :question_id, :trueoption, :question_attributes

  belongs_to :question

  accepts_nested_attributes_for :question
end

So basically, what it does is it saves only 1 result instead of each one seperately (nested form), but it should save a result for each question specifically.

Thanks in advance

Was it helpful?

Solution

In your model you have defined Result belongs to Question therefore only one question is stored for your result.

Update your relation :

result has_many question
 question belongs_to result
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top