1

我遵循了到目前为止有效的教程“通过文本字段创建模型”(Railscast #57) 。不过,我注意到新对象的选择字段输入字段都没有得到验证。

我正在使用accepts_nested_attributes_forvalidates受影响的模型。在我添加表单字段之前一切正常。现在,当我提交表单而不选择输入任何内容时,不会出现任何警告。这当然会产生错误的数据库条目。

编辑1:我添加了表格和实习模型的屏幕截图。

一个例子:在创建一个新的实习时,我希望能够选择一家公司 创建一个新的公司。我按照教程中的描述添加了代码,但我不确定如何处理验证。
这里是实习模式

class Internship < ActiveRecord::Base

  belongs_to :study
  belongs_to :company

  attr_accessor :new_company_name, :new_company_website
  before_save :create_company_from_data

  accepts_nested_attributes_for :company, :study

  validates :from, :presence => true
  validates :till, :presence => true
  validates_associated :company, :study

  def create_company_from_data
    create_company(:name => new_company_name, :website => new_company_website, :kind => false) unless new_company_name.blank?
  end

end

我正在使用 Rails 3.0.5。

4

1 回答 1

0

这对我有用。

这是实习模型。

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 字段位于实习模型中,而是位于它们所属的设施模型中。

于 2011-04-14T13:15:48.890 回答