Rails 3.2.12不能批量分配受保护的属性

问题描述:

这是我的RubyOnRails应用程序。我有一个ActiveRecord:Rails 3.2.12不能批量分配受保护的属性

class Deposit < ActiveRecord::Base 
    attr_accessible :fk, :abs_kod, :neres 
    ... 
end 

我也有代码控制器:

list.each do |d_item| 
     deposit = Deposit.find_or_create_by_fk(d_item[:fk]) 
     deposit.update_attributes d_item 
end 

但我不能任意值保存到“neres”字段。我得到一个错误

Can't mass-assign protected attributes: neres 

,并在我的数据库中,我看到

| fk | abs_kod | neres | 
| 1 | 1  |  | 

我试图做这样的

deposit = Deposit.find_or_create_by_fk(d_item[:fk]) 
deposit.neres = d_item[:neres] 
deposit.update_attributes d_item 
deposit.neres = d_item[:neres] //twice, just in case 

我没有帮助。另外我试图从活动记录中删除attr_accessible。它也没有帮助。有什么问题?

P.S .: Attribure“neres”被添加晚于“abs_kod”和“fk”。

要使用Rails的强大的应用程式PARAMS小于4版本,则需要宝石添加到您的宝石文件

gem 'strong_parameters' 

,并在配置组whitelist_attributes application.rb中的文件,以虚假

config.active_record.whitelist_attributes = false 

在控制器中,一定要添加一个model_params方法并列出需要列入白名单的属性。