如何通过文本字段创建模型并进行验证?

时间:2011-03-27 23:25:12

标签: ruby-on-rails validation nested-forms railscasts

我遵循了迄今为止有效的教程"Create a model through text field" (Railscast #57)。但是,我注意到选择字段和新对象的输入字段都没有得到验证。

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

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

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

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。

1 个答案:

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