2013-05-14 130 views
1

我是很新,整个轨道MVC的概念,我一直在负责创建形式执行以下操作:轨动态生成表单

  • 需要一个测试号
  • 展示了如何每个测试中有很多部分(这是一个常数,总是4)
  • 允许用户在某个区域中输入每个问题的答案。每个部分中的问题数量根据所获得的测试编号而变化。

我有我的观点,看起来像这样:

= form_for [@answer_sheet] do |f| 
     =f.collection_select(:test_prep_number, Exam.find(:all),:test_prep_number, :test_prep_number, {:include_blank => 'Select your test prep number'}) 
     =f.fields_for :answer_sections do |section_form| 
     =section_form.label :section 
       .form-inline 
      =f.label :A 
      =radio_button_tag 'answer', 'A' 
      =f.label :B 
      =radio_button_tag 'answer', 'B' 
      =f.label :C 
      =radio_button_tag 'answer', 'C' 
      =f.label :D 
      =radio_button_tag 'answer', 'D' 
      =f.label :E 
      =radio_button_tag 'answer', 'E' 

我的控制器看起来是这样的:

def index 
    @answer_sheet = AnswerSheet.build_with_answer_sections 
    @answer_section = AnswerSection.new 
    @section_count = AnswerSection.where("exam_id = ?", params[:test_prep_number).count 
end 

我有现在的问题是,我似乎无法包裹我的头围绕创建正确数量的单选按钮。到目前为止,我已经设法只为每个部分生成一个问题。

我假设我需要一个for循环(然后需要查询来查找每个考试部分有多少个问题)。

编辑:添加模型的要求

答卷模式

class AnswerSheet < ActiveRecord::Base 
    attr_accessible :date, :raw_score, :test_prep_number, :answer_sections, answer_sections_attributes 
MAX = 101 
validates :test_prep_number, :presence => true 
validates :raw_score, :presence => true, :numericality => { :greater_than_or_equal_to => 0, :less_than_or_equal_to => MAX} 
validates :date, :timeliness => {:on_or_before => lambda { Date.current }, :type => :date}, :presence => true 
belongs_to :user 
has_many :answer_sections 

回答部分型号

class AnswerSection < ActiveRecord::Base 
    MAX = 30 

    attr_accessible :section_score, :answers, :answer_attributes 
    has_many :answers, :dependent => :destroy 
    belongs_to :answer_sheet 
    accepts_nested_attributes_for :answers 

    validates :section_score, :presence => true, :numericality => { :greater_than_or_equal_to => 0, 
:less_than_or_equal_to => MAX } 
+0

AnswerSheet和AnswerSection模型之间的关系是什么? – eabraham

+0

AnswerSheet包含很多AnswerSections,每个AnswerSection都属于一个AnswerSheet。 – docaholic

+0

你可以跟我分享这两个模型吗? – eabraham

回答

0

我会建议调整模型东西断裂的选择来自用户回答:

class Test < ActiveRecord::Base 
    belongs_to :user 
    has_many :question 

    attr_accessible :date, :test_prep_number 

    validates :test_prep_number, :presence => true 
    validates :date, :timeliness => {:on_or_before => lambda { Date.current }, :type => :date}, :presence => true 
end 

class Question < ActiveRecord::Base 
    MAX = 30 

    attr_accessible :question_text 
    has_many :choices, :dependent => :destroy 
    has_one :answer 
    belongs_to :test 
    accepts_nested_attributes_for :answers 

end 

class Choice < ActiveRecord::Base 
    attr_accessible :question_id, :choice_text, :correct_choice 
    belongs_to :question 
end 

class Answer < ActiveRecord::Base 
    attr_accessible :choice_id 
    belongs_to :user 
    belongs_to :question 
end