2011-03-27 56 views
1

我跟着教程"Create a model through text field" (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。

+0

請顯示一些代碼。通過互聯網很難使用心靈閱讀權力。 – nathanvda 2011-03-28 06:50:37

+0

謝謝:它使我/我們不必看截屏。你正在使用哪種導軌版本? – nathanvda 2011-03-29 09:33:37

+0

我正在使用Rails 3.0.5。 – JJD 2011-03-29 09:57:06

回答

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場坐在實習模式,而不是在它們所屬的設施模型。

相關問題