我正在使用 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
所以,我认为 a 可以构建嵌套形式。这是表单的代码
<%= 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
最后是控制器:
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
发生了几件事。首先,我有一家有两个地址的公司,我可以编辑第一个地址,第二个地址的任何更改都不会保留。然后地址字段没有被验证(如果我在再次打开表单时将所有内容都留空,地址没有保存,我可以看到原始值)。并且当编辑任意地址的字段时,公司的任意字段值无效,提交表单后我可以看到公司模型上的错误,但地址显示为原始值,所以编辑的值是丢失。
希望清楚。
提前致谢。