Ruby on rails 当我在activeadmin中单击“创建”时,我得到一个质量分配错误

Ruby on rails 当我在activeadmin中单击“创建”时,我得到一个质量分配错误,ruby-on-rails,ruby-on-rails-3,activeadmin,nested-forms,Ruby On Rails,Ruby On Rails 3,Activeadmin,Nested Forms,所以我有主动管理员。我有一个客户模型和一个地址模型。我的地址嵌套在客户中。当我单击“创建客户”时,我得到一个质量分配错误 错误 客户模型 class Customer < ActiveRecord::Base attr_accessible :name, :email, :phone, :addresses_attributes has_many :addresses accepts_nested_attributes_for :addresses, :allow_dest

所以我有主动管理员。我有一个客户模型和一个地址模型。我的地址嵌套在客户中。当我单击“创建客户”时,我得到一个质量分配错误

错误

客户模型

class Customer < ActiveRecord::Base
    attr_accessible :name, :email, :phone, :addresses_attributes
  has_many :addresses
  accepts_nested_attributes_for :addresses, :allow_destroy => true
end
class Customer < ActiveRecord::Base
   attr_accessible :name, :email, :phone, :addresses_attributes
  has_many :addresses
  accepts_nested_attributes_for :addresses, :allow_destroy => true
end
class Customer < ActiveRecord::Base
   attr_accessible :name, :email, :phone, :address_attributes
  has_one :address
  accepts_nested_attributes_for :address, :allow_destroy => true
end
views/admin/customers/_form.html.erb

  <%=
semantic_form_for [:admin, @customer], :builder => ActiveAdmin::FormBuilder do |f| 
      f.inputs "Customer Information" do 
        f.input :name 
        f.input :email
        f.input :phone
        end 
        render :partial => "admin/addresses/form", :locals => { :form => f } 
          f.buttons 
    end
%>
<%=
form.inputs "Address" do 
  form.semantic_fields_for :address do |address| 
    address.inputs :class => "" do 
      address.input :street 
      address.input :city 
      address.input :state
      address.input :zip, as: :string
    end 
  end 
end 
%>
ActiveAdmin::FormBuilder do | f |
f、 输入“客户信息”吗
f、 输入:姓名
f、 输入:电子邮件
f、 输入:电话
结束
render:partial=>“admin/addresses/form”,:locals=>{:form=>f}
f、 钮扣
结束
%>
views/admin/addresses/_form.html.erb

  <%=
semantic_form_for [:admin, @customer], :builder => ActiveAdmin::FormBuilder do |f| 
      f.inputs "Customer Information" do 
        f.input :name 
        f.input :email
        f.input :phone
        end 
        render :partial => "admin/addresses/form", :locals => { :form => f } 
          f.buttons 
    end
%>
<%=
form.inputs "Address" do 
  form.semantic_fields_for :address do |address| 
    address.inputs :class => "" do 
      address.input :street 
      address.input :city 
      address.input :state
      address.input :zip, as: :string
    end 
  end 
end 
%>
“做什么 地址.输入:街道 地址.输入:城市 地址输入:state address.input:zip,as::string 结束 结束 结束 %> views/admin/addresses/_show.html.erb

<div class="panel">
    <h3>Address</h3>
    <div class="panel_contents">
      <div id="attributes_table_employee_1" class="attributes_table">
        <table cellspacing="0" cellpadding="0" border="0">
          <tbody>
          <tr>
            <th>Street</th>
            <td><%= address.street.blank? ? raw('<span class="empty"></span>') : address.street %></td>
          </tr>
          <tr>
            <th>City</th>
            <td><%= address.city.blank? ? raw('<span class="empty">Empty</span>') : address.city %></td>
          </tr>
          <tr>
            <th>State</th>
            <td><%= address.state.blank? ? raw('<span class="empty">Empty</span>') : address.state %></td>
          </tr>
          <tr>
            <th>Zip</th>
            <td><%= address.zip.blank? ? raw('<span class="empty">Empty</span>') : address.zip %></td>
          </tr>
        </tbody></table>
      </div>
    </div>
  </div>

地址
街头
城市
陈述
拉链

在地址模型中,您不需要在attr\u accessible方法中引用客户id,也不需要声明客户id

您已经使用“属于”和“拥有”设置了两个模型之间的关系

确保您有:

t、 参考资料:联系方式


在您的地址迁移中。

您确实为has\u-one关系构建表单,但您有多个has\u-one关系

 f.inputs "Addresses" do
        f.has_many :addresses do |t|
             t.input :street 
             t.input :city 
             t.input :state
             t.input :zip, as: :string

      end
end
在这种情况下,您的模型应该 像

如果您有一个关系,则将模型声明更改为

客户模型

class Customer < ActiveRecord::Base
    attr_accessible :name, :email, :phone, :addresses_attributes
  has_many :addresses
  accepts_nested_attributes_for :addresses, :allow_destroy => true
end
class Customer < ActiveRecord::Base
   attr_accessible :name, :email, :phone, :addresses_attributes
  has_many :addresses
  accepts_nested_attributes_for :addresses, :allow_destroy => true
end
class Customer < ActiveRecord::Base
   attr_accessible :name, :email, :phone, :address_attributes
  has_one :address
  accepts_nested_attributes_for :address, :allow_destroy => true
end
class客户true
结束
地址模型

class Address < ActiveRecord::Base
    attr_accessible :street, :city, :state, :zip, :customer_id
  belongs_to :customer
  has_one :customer_id
end
class Address < ActiveRecord::Base
  attr_accessible :street, :city, :state, :zip, :customer_id
  belongs_to :customer
end
class Address < ActiveRecord::Base
   attr_accessible :street, :city, :state, :zip, :customer_id
  belongs_to :customer
end
类地址
查看下一个问题/答案以了解更多示例

如果您有一个关系,则将模型声明更改为

客户模型

class Customer < ActiveRecord::Base
    attr_accessible :name, :email, :phone, :addresses_attributes
  has_many :addresses
  accepts_nested_attributes_for :addresses, :allow_destroy => true
end
class Customer < ActiveRecord::Base
   attr_accessible :name, :email, :phone, :addresses_attributes
  has_many :addresses
  accepts_nested_attributes_for :addresses, :allow_destroy => true
end
class Customer < ActiveRecord::Base
   attr_accessible :name, :email, :phone, :address_attributes
  has_one :address
  accepts_nested_attributes_for :address, :allow_destroy => true
end
class客户true的\u嵌套\u属性
结束
地址模型

class Address < ActiveRecord::Base
    attr_accessible :street, :city, :state, :zip, :customer_id
  belongs_to :customer
  has_one :customer_id
end
class Address < ActiveRecord::Base
  attr_accessible :street, :city, :state, :zip, :customer_id
  belongs_to :customer
end
class Address < ActiveRecord::Base
   attr_accessible :street, :city, :state, :zip, :customer_id
  belongs_to :customer
end
类地址
如果出现任何错误,请在问题中包含您得到的确切错误消息:)我的迁移中没有该消息。我该怎么做呢?您可以编辑迁移文件以获得污垢解决方案,或者创建新迁移以添加新列。如果您使用的是edge rails(4.0),那么您可以使用:rails生成迁移AddAddressRefToContacts联系人:引用,正如您在文档中看到的:我假设您说的联系人是指客户