CustomersMtroller中的ActiveModel :: MassAssignmentSecurity :: Error #create(attr_accessible已设置)

时间:2012-07-04 04:50:17

标签: ruby-on-rails activerecord mass-assignment

在我的控制器中,我在创建动作时遇到错误并尝试创建模型[无法进行质量分配],但是 在我的规范中,我的质量分配模型的测试通过了!?!

我的模特:

class Customer < ActiveRecord::Base
  attr_accessible :doc, :doc_rg, :name, :birthday, :name_sec, :address, :state_id, :city_id, :district_id,
                  :customer_pj, :is_customer, :segment_id, :activity_id, :person_type, :person_id

  belongs_to    :person , :polymorphic => true, dependent: :destroy
  has_many :histories
  has_many :emails

  def self.search(search)
    if search
      conditions = []
      conditions << ['name LIKE ?', "%#{search}%"]
      find(:all, :conditions => conditions)
    else
      find(:all)
    end
  end 
end

我已经厌倦了在控制器中设置attr_accessible,以我的随机方式。

控制器:

class CustomersController < ApplicationController
  include ActiveModel::MassAssignmentSecurity
  attr_accessible :doc, :doc_rg, :name, :birthday, :name_sec, :address, :state_id, :city_id, :district_id, :customer_pj, :is_customer

  autocomplete :business_segment, :name, :full => true
  autocomplete :business_activity, :name, :full => true

  [...]

end

测试,我通过测试

  describe "accessible attributes" do
  it "should allow access to basics fields" do
    expect do
      @customer.save
    end.should_not raise_error(ActiveModel::MassAssignmentSecurity::Error)
  end    
end

错误:

ActiveModel::MassAssignmentSecurity::Error in CustomersController#create
Can't mass-assign protected attributes: doc, doc_rg, name_sec, address, state_id, city_id, district_id, customer_pj, is_customer

https://github.com/megabga/crm

1.9.2p320

Rails 3.2

的MacOS

PG

1 个答案:

答案 0 :(得分:1)

我的不好,在我的控制器中它设置了一个最古老的课程。然后旧类没有传递参数的属性。遗憾!