2013-05-14 359 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