2016-11-06 87 views
0

我正在開發一個網站,它將主持針對任何主題的不同測試。 我正在使用嵌套窗體進行引用。 考試有問題,一個問題有4個選項,每個問題都有正確的答案。 模型是這樣的:指導創建Rails應用程序

class Exam < ActiveRecord::Base 
    has_many :questions 
    validates :name, presence: true 
    accepts_nested_attributes_for :questions, 
    reject_if: proc  {|attributes| attributes['content'].blank?}, 
           allow_destroy: true 
    end 

    class Question < ActiveRecord::Base 
    belongs_to :exam 
    has_many :correct_answers 
    validates :content, presence: true 
    has_many :options 
    accepts_nested_attributes_for :options, 
    reject_if: proc  {|attributes| attributes['content'].blank?}, 
           allow_destroy: true 
    accepts_nested_attributes_for :correct_answers, reject_if: proc {|attributes| attributes['content'].blank?}, 
           allow_destroy: true        
    end 

    class Option < ActiveRecord::Base 
     belongs_to :question 
    end 

現在我不能夠了解如何創建一個Web表單,可以提交結果,可以比較正確的答案選擇的選擇,然後渲染與正確答案的觀點和選定的答案。

請幫忙。

回答

0

我想你可以用這樣的東西很簡單:如果您需要任何進一步的幫助,你的表單

<%= form_for(@exam) do |f| %> 
    <%= f.label :name %> 
    <%= f.text_field :name %> 
    <%= f.label :question %> 
    <%= f.text_field :question%> 
    <%= f.submit Submit %> 

FYI剛讀這篇文章 - >https://www.launchacademy.com/codecabulary/learn-rails/writing-forms

+0

感謝您的答覆!但是,這樣,考試模型的參數將被提交。我希望顯示考試的內容(問題和選項),他們提交相同的內容。之後,我會將標記的答案與正確答案模型進行比較。 – shivank