2011-09-17 172 views
2

我無法創建要編輯的表單StudentReferrals。每個學生都有一些基本信息,如姓名/生日/學習。除了這些信息之外,學生還可以參考一下課程清單。我想要一個表格,允許有人填寫學生信息,然後選擇最多10個學生需要參加的課程。除了課程外,每個「學生+課程」組合還有一個credits_required字段,表示學生需要參加的課程的學分數。Rails中嵌套對象的形式

的使學生在上課的列表來自Course模型

我有3種型號:

class StudentReferral < ActiveRecord::Base 
    has_many :student_referral_courses, :dependent => :destroy 
    has_many :courses, :through => :student_referral_courses 

    # Fields: name, studentid, advisor_name, birthday 
end 

class StudentReferralCourse < ActiveRecord::Base 
    belongs_to :student_referral 
    belongs_to :course 

    validates_presence_of :credits_required 

    # Fields: (student_referral_id, course_id, credits_required) 

    attr_accessor :course1, ..., :course9 
    attr_accessor :credits1, ..., :credits9 

    after_create :add_courses 

    def add_courses 

    self.student_referral_courses.destroy_all 

    unless self.course1.blank? 
     self.student_referral_courses.create! :course_id => self.course1, :student_referral_id => self.id, :credits_required => self.credits1 
    end 
    unless self.course2.blank? 
     self.student_referral_courses.create! :course_id => self.course2, :student_referral_id => self.id, :credits_required => self.credits2 
    end 
    ... 
end 

class Course < ActiveRecord::Base 
    # Fields: department, name 
end 

而且我有一個表格,以創建/編輯的學生轉介是像以下:

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

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



<fieldset class="form"> 
<legend>Student Information</legend> 
<table cellpadding="3" cellspacing="0" border="0" class="vert"> 
<tr><th class="subr"><%= f.label :studentid %></th><td><%= f.text_field :studentid %></td></tr> 
<tr><th class="subr"><%= f.label :name %></th><td><%= f.text_field :name %></td></tr> 
<tr><th class="subr"><%= f.label :birthday %></th><td><%= f.date_select :birthday, :start_year => 1990 %></td></tr> 
<tr><th class="subr"><%= f.label :advisor %></th><td><%= f.text_field :advisor %></td></tr> 
</table> 
</fieldset> 


<fieldset class="form"> 
<legend>Course Information</legend> 
<span>You <b>MUST</b> select the credits required for each course.</span> 
<ol> 
<li><%= select(:student_referral, :course1, Course.all(:order => "dept,name ASC").collect {|p| [ "#{p.dept}: #{p.name}", p.id ] }, { :include_blank => '' })%> 
    <%= select_tag 'student_referral[credits1]', options_for_select(['Select One',5.0,4.5,4.0,3.5,3.0,2.5,2.0,1.5,1.0,0.5]) %></li> 
<li><%= select(:student_referral, :course2, Course.all(:order => "dept,name ASC").collect {|p| [ "#{p.dept}: #{p.name}", p.id ] }, { :include_blank => '' })%> 
    <%= select_tag 'student_referral[credits2]', options_for_select(['Select One',5.0,4.5,4.0,3.5,3.0,2.5,2.0,1.5,1.0,0.5]) %></li> 
    ... 
</ol> 
</fieldset> 

<%= f.submit :class => "l-button black" %> 

<% end %> 

我的問題是,我似乎無法創建或沒有做使用驗證一個可怕的黑客編輯這些記錄。我有course1course9的10 attr_accessible字段和我在after_create宏上使用的信用相同。然而,這並不讓我看到課程的任何驗證錯誤(如他們沒有放任何學分)

我試過使用accepts_nested_attributes_for :student_referral_courses,但我似乎無法讓它做我想做的事。我真的想用Rails的方式來做這件事,我覺得我正在做一個可怕的黑客攻擊。

+0

這可能會有所幫助:http://railscasts.com/episodes/73-complex-forms-part-1 – jefflunt

+0

@normalocity - 我已經嘗試過,但我似乎無法讓它工作。 –

回答

0

嘗試添加的has_many:通過對課程即

class Course < ActiveRecord::Base
has_many :student_referrals, :dependent => :destroy
has_many :student_referral_courses, :through => :student_referrals
end

+0

已更新2011/11/22 –