我做错了或accepts_nested_attributes_for
或fields_for
我使用rails 3.0.3。
我有2个模特
class Customer < ActiveRecord::Base
belongs_to :address
accepts_nested_attributes_for :address
end
class Address < ActiveRecord::Base
has_many :customers
end
在我的_form.html.haml
= f.fields_for :address do |a|
= a.label :street, t("activerecord.attributes.address.street", :default => 'Street'), :class => :label
= a.text_field :street, :class => 'text_field'
当我获取/customers/new
时,街道输入字段的HTML源代码为
<input class="text_field" id="customer_address_street" name="customer[address][street]" size="30" type="text">
当我获取/customers/1/edit
时,街道输入字段的HTML源代码为
<input class="text_field" id="customer_address_attributes_street" name="customer[address_attributes][street]" size="30" type="text" value="...">
编辑正确且工作正常,但新错误,ID和NAME中缺少_attributes
。
有什么想法吗?
答案 0 :(得分:3)
我发现了我的错误
在new.html.haml
我得到了
= form_for :customer, :url => customers_path, :html => { :class => :form } do |f|
使其有效我将:customer
替换@customer