代码之家  ›  专栏  ›  技术社区  ›  denisjacquemin

新操作中缺少字段和属性的问题

  •  2
  • denisjacquemin  · 技术社区  · 15 年前

    我做错了什么或者是有问题 accepts_nested_attributes_for 或 fields_for

    我使用rails 3.0.3。

    我有两个模特

    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 street输入字段的HTML源代码是

    <input class="text_field" id="customer_address_street" name="customer[address][street]" size="30" type="text">
    

    当我得到 /customers/1/edit street输入字段的HTML源代码是

    <input class="text_field" id="customer_address_attributes_street" name="customer[address_attributes][street]" size="30" type="text" value="...">
    

    编辑是正确的,工作正常,但新的是错误的 _attributes ID和名称中缺少。

    知道吗?

    1 回复  |  直到 15 年前
        1
  •  3
  •   denisjacquemin    15 年前

    我发现了我的错误

    在 new.html.haml 我得到了

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

    为了让它工作,我必须更换 :customer 通过 @customer