我正在使用rails 3.2并且我正在构建嵌套表单。但事情并没有像我预期的那样奏效。首先,我的模型是一个有很多地址的公司。这是模型
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%>
我有一个简单的帮助器来生成带有bootstrap的表单字段
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
最终控制者:
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
发生了什么事情。首先,我有一个有两个地址的公司,我能够编辑第一个,第二个的任何更改都不会保留。然后地址字段未经验证(如果我再次打开表单时将所有内容留空,则地址不会保存,我可以看到原始值)。当编辑任何地址中的字段,并且公司的任何字段值无效时,在提交表单后,我可以看到公司模型上的错误,但地址显示的是原始值,因此编辑的值是丢失。
希望明确。
提前致谢。
答案 0 :(得分:2)
好吧,我找到了答案。我使用的是mongoid 3.0.4的版本。我运行命令bundle update mongoid,mongoid更新到3.0.6版。问题得到解决。
感谢。希望它有所帮助