简体   繁体   English

奇怪的“无法批量分配受保护的属性”错误

[英]Weird “Can't mass-assign protected attributes” error

Im running into the following error 我遇到以下错误

Can't mass-assign protected attributes:

I know this is a generally asked question, but none of the answers provided seem to work for me. 我知道这是一个普遍提出的问题,但是所提供的答案似乎都不适合我。 The thing is, i already have this 2 lines in my "parent" model: 问题是,我的“父代”模型中已经有了这2行:

accepts_nested_attributes_for

AND

attr_accessible  :childmodel_attributes

I had run into this error in the past, and those 2 lines succefully solved the issue.. But they dont now. 我过去曾遇到此错误,而这两行成功地解决了这个问题。但是现在它们还没有。 BTW, the "protected attributes" are ALL the fields in my "child" model, not just certain ones. 顺便说一句,“受保护的属性”是我的“孩子”模型中的所有字段,而不仅仅是某些字段。

Hope someone can help me, im stuck and dont know what else to do. 希望有人可以帮助我,我陷入困境,不知道该怎么办。

This is the full error line: 这是完整的错误行:

Can't mass-assign protected attributes: linea, origen_comp, conector, char_ini, char_fin, modopremio_id, codigo_opc, ochar_ini, ochar_fin

This is what the "child_model" has 这就是“ child_model”的内容

:consorcio_id, :productosacierto_id, :clave, :linea, :origen_comp, :conector, :char_inic, :char_fin, :modopremio_id, codigo_opc, :ochar_ini, :onchar_fin

(Sidenote, the "parent/child model" names are just for reference, they are not their true names (productosacierto AND productosregla) would be it. (旁注,“父/子模型”的名称仅供参考,它们并不是自己的真实姓名(productosacierto和productosregla)将是它。

Parent Model (Productosacierto) 父模型(Productosacierto)

has_many :productosregla
accepts_nested_attributes_for :productosregla, :reject_if => :all_blank, :allow_destroy => true
attr_accessible  :productosregla_attributes, :producto_id, :consorcio_id, :clave, :descripcion, :una_condicion
default_scope order: 'id'
self.table_name = "hproductos_aciertos"

Child Model (Productosregla) 子模型(Productosregla)

self.table_name = "hproductos_reglas"  
belongs_to :productosacierto
attr_accessible :consorcio_id, :productosacierto_id, :clave, :linea, :origen_comp, :conector, :char_inic, :char_fin, :modopremio_id, codigo_opc, :ochar_ini, :ochar_fin 
default_scope order: 'id'

如果这是您的子模型的精确副本,那么我想attr_accessible行末尾的逗号就是问题所在!

I feel kind of embarrassed right now. 我现在有点尴尬。 There were several sintax errors on my model and thats what was causing this issue. 我的模型上有多个sintax错误,这就是导致此问题的原因。 I took it for granted that rails would throw a sintax error if there were some, specially when it's so delicate when it comes to writting code in the model... 我理所当然地认为,如果有一些错误的话,rails会抛出sintax错误,特别是当在模型中编写代码时如此之细的时候...

This is what i had: 这就是我所拥有的:

attr_accessible :consorcio_id, :productosacierto_id, :clave, :linea, :origen_comp, :conector, :char_inic, :char_fin, :modopremio_id, codigo_opc, :ochar_ini, :ochar_fin

This is what it was supposed to be: 这是应该的:

attr_accessible :consorcio_id, :productosacierto_id, :clave, :linea, :origen_comp, :conector, :char_ini, :char_fin, :modopremio_id, :codigo_opc, :ochar_ini, :ochar_fin 

As you can see, there was a ":" missing before the codigo_opc param, and also the char_ini param had an extra "c" that didnt belong. 如您所见,在codigo_opc参数之前缺少一个“:”,并且char_ini参数还有一个不属于的额外“ c”。 Thanks everyone who helped, this taught me to make sure never to assume anything. 感谢所有帮助的人,这教会了我确保永远不要承担任何责任。

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

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