1

在我的控制器中,我在创建动作并尝试创建模型时出错 [无法批量分配],但在我的规范中,我对批量分配模型的测试通过了!?!

我的模型:

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

导轨 3.2

苹果系统

皮克

4

1 回答 1

1

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

于 2012-07-05T03:11:12.450 回答