简体   繁体   English

Rails 4无法批量分配attr_accessor方法

[英]Rails 4 cant mass assign attr_accessor method

I have the following model: 我有以下模型:

class Something < ActiveRecord::Base
   attr_accessor :properties
   serialize :properties
end

When I call: 当我打电话时:

thing = Something.new({name:"foo",properties:{key_1:"val"}})

thing has name populated but not properties. 事物具有填充的名称,但没有属性。 I can manually assign properties as such: 我可以这样手动分配属性:

thing.properties = {key_1:"val"}

and that works fine, but I cannot make it work through mass assignment. 效果很好,但我无法通过批量分配使它起作用。

Am I missing something? 我想念什么吗?

Rails 4 uses strong parameters in the controller. Rails 4在控制器中使用强参数 So while having your attrs set in the model, you still have to permit params in the controller. 因此,虽然在模型中设置了属性,但仍必须permit控制器中包含参数。

This is the line to look for/add into your controller: 这是要查找/添加到控制器中的行:

def person_params
  params.require(:thing).permit(:parm1, :param2)
end

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM