这对我有用。
这是实习模型。
class Internship < ActiveRecord::Base
attr_accessor :new_company_name, :new_company_website
belongs_to :user
belongs_to :study
belongs_to :company, :class_name => "Facility", :foreign_key => 'facility_id'
has_one :student, :through => :study
accepts_nested_attributes_for :company, :study
validates :from, :presence => true
validates :till, :presence => true
validates_presence_of :study
validates_presence_of :company, :unless => :new_company_given?
validates_presence_of :new_company_name, :new_company_website, :unless => :company_given?
before_save :create_company_from_data, :if => :new_company_given?
private
# Returns a boolean value indicating whether a company is available.
# @return [Boolean] True if a company is available; otherwise false.
def company_given?
return self.facility_id.present?
end
# Returns a boolean value indicating whether a new_company is available.
# @return [Boolean] True if a new_company is available; otherwise false.
def new_company_given?
return (self.new_company_name.present? or self.new_company_website.present?)
end
# Creates a new company object taking into account the given parameters.
def create_company_from_data
create_company(:name => new_company_name, :website => new_company_website, :kind => false)
end
end
这是新实习形式的部分形式。
<%= error_messages_for @internship %>
<div class="formbox">
<div class="formseparator">When?</div>
<%= render :partial => "shared/internship_form_fields", :locals => { :internship_fields => internship_fields } %>
<div class="formseparator">Where?</div>
<p>
<span class="formlabel"><%= internship_fields.label :facility_id, "Company" %></span>
<span class="formvalue"><%= internship_fields.select :facility_id, Facility.companies.sorted.collect { |c| [c.name, c.id] }, { :include_blank => "Please select a company." }, { :class => "single" } %></span>
</p>
<p>
<span class="formlabel"><%= internship_fields.label :new_company_name, "or create a new", :class => "optional" %></span>
<span class="formvalue"><%= internship_fields.text_field :new_company_name, :class => "input" %></span>
</p>
<p>
<span class="formlabel"><%= internship_fields.label :new_company_website, "with a website", :class => "optional" %></span>
<span class="formvalue"><%= internship_fields.url_field :new_company_website, :class => "input" %></span>
</p>
<!-- End of company_fields -->
<div class="formseparator">Your study?</div>
<p>
<span class="formlabel"><%= internship_fields.label :study_id, "Study" %></span>
<% mystudies = Array.new %>
<% studies = Study.sorted.collect %>
<% studies.each do |study| %>
<% if (study.student == current_user) %>
<% mystudies.push(study) %>
<% end %>
<% end %>
<span class="formvalue">
<%= internship_fields.select :study_id, mystudies.collect { |s| [s.study_text, s.id] }, { :include_blank => "Please select one of your studies." }, { :class => "single" } %></span>
</p>
<!-- End of study_fields -->
</div>
<div class="form-buttons"><%= internship_fields.submit "Submit" %></div>
不过,我不喜欢 new_company 字段位于实习模型中,而是位于它们所属的设施模型中。