Rails测验应用获得结果

时间:2014-01-14 15:55:12

标签: ruby-on-rails radio-button nested-forms nested-attributes form-for

我正在尝试制作rails测验应用程序并遇到问题。 我正在使用nested_forms gem,我通常可以创建测验。 以下是创建测验的代码:

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

这很有效,测验被创建并保存。 但是当试图解决测验时,问题就出现了。 当我进行测验的展示视图(如测验/ 8)时,我正在通过这个显示测验:

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

它会正常显示每个问题,就像它应该的那样。但是什么不起作用的是单选按钮;它们就像它们一样,只有1个可以检出。有什么问题?

以下是控制器:

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

结果控制器:

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

模特:

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

所以基本上,它的作用是只保存1个结果而不是每个结果(嵌套形式),但它应该特别为每个问题保存结果。

提前致谢

1 个答案:

答案 0 :(得分:0)

在您的模型中,您已定义Result belongs to Question,因此只会为您的结果存储一个问题。

更新您的关系:

result has_many question
 question belongs_to result