2

所以我有活跃的管理员。我有一个客户模型和一个地址模型。我有嵌套在客户中的地址。当我单击创建客户时,出现批量分配错误。

错误

ActiveModel::MassAssignmentSecurity::Error in Admin::CustomersController#create

Can't mass-assign protected attributes: address

客户模型

class Customer < ActiveRecord::Base
    attr_accessible :name, :email, :phone, :addresses_attributes
  has_many :addresses
  accepts_nested_attributes_for :addresses, :allow_destroy => true
end

地址模型

class Address < ActiveRecord::Base
    attr_accessible :street, :city, :state, :zip, :customer_id
  belongs_to :customer
  has_one :customer_id
end

客户控制器

ActiveAdmin.register Customer do
    # Menu item
  menu :label => "Customers", :parent => "Administration"

  filter :name
  filter :created_at
  filter :updated_at


  index do
    column :name
  end

    form :partial => "form"

  show :title => :name do
      panel "Customer Details" do
          attributes_table_for resource do
            row :name
            row :email
            row :phone
          end
        text_node(render :partial => "admin/addresses/show", :locals => { :address => resource.address })
      end
    end
end

意见/管理员/客户/_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.html.erb

<%=
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 
%>

意见/管理/地址/_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>
4

2 回答 2

0

在您的 Address 模型中,您不需要在 attr_accessible 方法中引用 customer_id,也不需要声明 customer_id。

您已经使用 belongs_to 和 has_may 设置了两个模型之间的关系。

确保你有:

t.references:联系方式

在您的地址迁移中。

于 2013-05-02T21:28:40.420 回答
0

您确实为 has_one 关系构建表单,但您有 has_many 一个。

 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

在这种情况下,您的模型应该看起来像

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

客户模型

class Customer < ActiveRecord::Base
   attr_accessible :name, :email, :phone, :addresses_attributes
  has_many :addresses
  accepts_nested_attributes_for :addresses, :allow_destroy => true
end

地址模型

class Address < ActiveRecord::Base
  attr_accessible :street, :city, :state, :zip, :customer_id
  belongs_to :customer
end

查看下一个问题/答案以获取更多示例 如何通过关联在使用 has_many 的模型上使用 ActiveAdmin?

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

客户模型

class Customer < ActiveRecord::Base
   attr_accessible :name, :email, :phone, :address_attributes
  has_one :address
  accepts_nested_attributes_for :address, :allow_destroy => true
end

地址模型

class Address < ActiveRecord::Base
   attr_accessible :street, :city, :state, :zip, :customer_id
  belongs_to :customer
end
于 2013-05-05T14:06:12.533 回答