2012-09-16 29 views
2

我使用rails 3.2和我建立一個嵌套的窗體。但事情並非如我所料。首先,我的模特是一個有很多地址的公司。這裏是模型mongoid和validates_associated不起作用

class Company 
    include Mongoid::Document 
    include Mongoid::Timestamps 

    field :name,      :type => String 
    field :description,   :type => String 
    field :order_minimun,  :type => Float 

    belongs_to :user 

    has_many :addresses 

    validates_presence_of :name, :description, :order_minimun 
    validates_length_of :name, minimum:2, maximum: 30 
    validates_length_of :description, minimum:5, maximum: 140 
    validates :order_minimun, :numericality => { :only_integer => true, :greater_than_or_equal_to => 0 } 

    accepts_nested_attributes_for :addresses 
    validates_associated :addresses 

end 

class Address 
    include Mongoid::Document 
    include Mongoid::Timestamps 
    include Mongoid::Spacial::Document 


    field :street,  :type => String 
    field :number,  :type => Integer 

    field :phone,   :type => String 

    field :location,  :type => Array,  spacial: {lat: :latitude, lng: :longitude, return_array: true } 

    embeds_many :delivery_zones 


    belongs_to :company 
    belongs_to :city 

    has_many :openingTimeRange 

    validates_presence_of :street, :number 
    validates_length_of :street, minimum:1, maximum: 30 
    validates_length_of :number, minimum:1, maximum: 6 
    validates_length_of :phone, minimum:5, maximum: 60 


    attr_accessible :street, :number, :company_id, :city_id, :location, :phone, :delivery_zones, :latitude, :longitude 

end 

正如你可以看到公司模型有:

accepts_nested_attributes_for :addresses 
validates_associated :addresses 

所以,我覺得可以建立一個嵌套的表格。這裏的形式爲

<%= form_for [:admin,@company],:url =>admin_company_path(@company), :html => {:class => "form-horizontal"} do |f|%> 

    <legend><%= t '.legend' %></legend> 

    <%= group_input_field f, :name%> 

    <%= group_field_for f, :description do%> 
     <%= f.text_area :description, :rows => 5%> 
    <% end -%> 

    <%= group_input_field f, :order_minimun%> 

    <%= f.fields_for :addresses do |builder|%> 
     <%= render 'address_fields', :f=> builder%> 
    <% end %> 

    <div class="form-actions"> 
     <%= f.submit :class => 'btn btn-primary btn-large', :disable_with => t('button.saving') %> 
     <%= link_to t('.cancel', :default => t("helpers.links.cancel")), 
       admin_companies_path, :class => 'btn btn-large btn-danger' %> 
    </div> 

<% end %> 

的_address_fields.html.erb的代碼

<%= group_input_field f, :street%> 
<%= group_input_field f, :number%> 

我有一個簡單的輔助,以產生具有自舉

def group_input_field(f,field, options={}) 
    has_error = f.object.errors.has_key? field 
    klass = has_error ? "control-group error": "control-group" 
    content_tag(:div, :class => klass) do 
     f.label(field, :class => 'control-label')+ 
     content_tag(:div, :class => 'controls') do 
      f.text_field(field, :class => 'input')+ 
      show_error(f.object,field,has_error)+ 
      show_help(options) 
      end 
     end 
end 

Finaly控制器表單字段:

class Admin::CompaniesController < ApplicationController 

    def new 
     #crea una nueva compañia 
     @company = Company.new 
    end 

    def edit 
     @company = Company.find params[:id] 
    end 

    def create 
     @company = Company.new(params[:company]) 
     if @company.save 
      redirect_to :action => 'index' 
     else 
      render 'new' 
     end 
    end 

    def update 
     @company = Company.find(params[:id]) 
     if @company.update_attributes(params[:company]) 
      redirect_to :action => 'index' 
     else 
      render 'edit' 
     end 
    end 

end 

發生了什麼事情。首先,我有一家有兩個地址的公司,我可以編輯第一個地址,但第二個地址的任何更改都不會被保留。然後,地址字段未經驗證(如果我將所有內容都留空,當我再次打開表單時,地址未保存,我可以看到原始值)。並且,當編輯任何地址的字段,並且公司的任何字段值無效時,在表單被提交後,我可以看到公司模型中的錯誤,但地址以原始值顯示,所以編輯的值是丟失。

希望清楚。

在此先感謝。

回答

2

嗯,我找到了答案。我使用的是mongoid 3.0.4的版本。我運行命令包更新mongoid和mongoid更新到3.0.6版本。問題得到解決。

謝謝。希望它有幫助