当我在activeadmin中单击create时,我收到了一个质量分配错误

时间:2013-05-02 19:03:54

标签: ruby-on-rails ruby-on-rails-3 activeadmin nested-forms

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

错误

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>

2 个答案:

答案 0 :(得分:0)

在地址模型中,您不需要在attr_accessible方法中引用customer_id,也不需要声明costumer_id。

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

确保您拥有:

  

参考:联系

在地址迁移中。

答案 1 :(得分: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

查看下一个问题/答案以获取更多示例 How to use ActiveAdmin on models using has_many through association?

如果您有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