在NEW操作中缺少fields_for,_attributes的问题

时间:2010-11-23 14:34:00

标签: ruby-on-rails

我做错了或accepts_nested_attributes_forfields_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

有什么想法吗?

1 个答案:

答案 0 :(得分:3)

我发现了我的错误

new.html.haml我得到了

= form_for :customer, :url => customers_path, :html => { :class => :form } do |f|

使其有效我将:customer替换@customer