markus_springer
markus_springer

Reputation: 174

Nested Form in Rails: questions answers

I have a problem with my rails application.

My models:
quiz: name
question: text, quiz_id
answer: text, question_id
right_answer: question_id, answer_id

A quiz has many questions. A single questions has many answers and a answer has only one right answer.

How can I solve this problem?

This is my form:

    <%= form_for([@quiz, @question]) do |f| %>
      <% if @question.errors.any? %>
        <div id="error_explanation">
          <h2><%= pluralize(@question.errors.count, "error") %> prohibited this question from being saved:</h2>

          <ul>
          <% @question.errors.full_messages.each do |message| %>
            <li><%= message %></li>
          <% end %>
          </ul>
        </div>
      <% end %>


      <div class="field">
        <%= f.label :text %><br>
        <%= f.text_field :text %>
      </div>

        <%= f.fields_for :answers do |u| %>
            <%= u.text_field :text, class: "form-control", id: "answer"%>
        <% end %>

        <div class="actions">
    <%= f.submit %>

I want to add a radio box to each answer to select which answer is the right.

Questions-Controller:

def new
  @question = Question.new
  @question.answers.build
end

  def create
    @question = Question.new(question_params)
    @question.quiz_id = @quiz.id
    i = 0
    until question_params[:answers_attributes].count
      @answer = @question.answers.new(question_params[:answers_attributes]["#{i}"])
      @answer.save
      i += 1
    end


    respond_to do |format|
      if @question.save
        format.html { redirect_to quiz_questions_path(@quiz), notice: 'Question was successfully created.' }
        format.json { render :show, status: :created, location: @question }
      else
        format.html { render :new }
        format.json { render json: @question.errors, status: :unprocessable_entity }
      end
    end
  end

How can I do this in my Controller and in my form, because I only have one right_answer but I need 4 radio buttons in my form?

Thanks

Upvotes: 1

Views: 1176

Answers (2)

Sharvy Ahmed
Sharvy Ahmed

Reputation: 7405

Add a new boolean field correct_answer in your answers table :

rails g migration add_correct_answer_to_answer correct_answer:boolean

questions_controller.rb

def new
  @question = Question.new
  4.times { @question.answers.build } # you can do it dynamically
end

def question_params
  params.require(:question).permit(:text, answers_attributes: [:id, :text, :correct_answer])
end

questions/_form.rb

<%= f.fields_for :answers do |builder| %>
  <%= render 'answer', f: builder %>
<% end %>

questions/_answer.html.erb

<%= f.label :text, "Answer" %>
<%= f.text_field :text %>
<%= f.radio_button :correct_answer %>

Then you can create a scope in question model to get correct answer of a particular question easily.

Upvotes: 1

Balakishore
Balakishore

Reputation: 66

= f.collection_radio_buttons :answer_id, @question.answers.all, :id, :name_with_initial

Upvotes: 1

Related Questions