Ruby on rails Can';t使用嵌套形式和自定义类名分配受保护的属性

Ruby on rails Can';t使用嵌套形式和自定义类名分配受保护的属性,ruby-on-rails,ruby-on-rails-3,nested-forms,nested-attributes,Ruby On Rails,Ruby On Rails 3,Nested Forms,Nested Attributes,我知道很多人已经有同样的问题,但我没有找到一个解决我的具体情况。 我有以下型号: class Invoicing::CommercialDocumentLine < ActiveRecord::Base attr_accessible :commercial_document_line_details_attributes has_many :details, :class_name => "Invoicing::CommercialDocumentLineDetail",

我知道很多人已经有同样的问题,但我没有找到一个解决我的具体情况。 我有以下型号:

class Invoicing::CommercialDocumentLine < ActiveRecord::Base
  attr_accessible :commercial_document_line_details_attributes

  has_many :details, :class_name => "Invoicing::CommercialDocumentLineDetail", :foreign_key => :commercial_document_line_id
  accepts_nested_attributes_for :details
end
问题是我一直有这样的警告:

WARNING: Can't mass-assign protected attributes: commercial_document_line_details
这是我的控制台日志:

"invoicing_commercial_document_line"=>{"article_id"=>"3", "unit_price"=>"200.0", "format_quantity"=>"2.0", "discount_rate"=>"0.0", "commercial_document_line_details"=>{"description"=>"awesome"}}, "commercial_document_id"=>"1", "id"=>"11"}
事实上,我对这里发生的事情有点困惑:如果我以我的形式替换

= f.simple_fields_for :commercial_document_line_details

那么表单将不会显示。如果我替换

attr_accessible :commercial_document_line_details_attributes

那么我也有同样的错误

我确实错过了一些东西,但我想不出来。

任何帮助都将不胜感激。

如果您将
=f.simple\u fields\u用于:details
,它实际上是在获取
f.object
的详细信息列表。对于新对象,这通常为空,因此您将看不到任何内容。这是使用字段的正确方法。您只需要在控制器中使用
@line.details.build
或在视图中使用
f.object.details.build

构建详细信息,如果您使用
=f.simple\u fields\u for:details
,它实际上是在获取
f.object
的详细信息列表。对于新对象,这通常为空,因此您将看不到任何内容。这是使用字段的正确方法。您只需在控制器中使用
@line.details.build
或在视图中使用
f.object.details.build

构建详细信息,谢谢您的回答,事实上,当我在控制器中添加
@line.details.build
时,你是对的,我现在可以看到与ann相关的详细信息,但当我更新一条线时,我一直有相同的警告。。。有什么想法吗?你是否应该使用
attr\u accessible:details\u attributes
?是的,就像我在问题中说的,我尝试了:details\u attributes和:commercial\u document\u line\u details\u attributes,这两种属性都会给我同样的错误,我不明白……如果你使用
f.simple\u fields\u for:details
,你应该收到一组不同的参数。它非常有效,我只是忘了更改它。非常感谢您的时间。感谢您的回答,事实上,我在控制器中添加
@line.details.build
时您是对的。我现在可以看到与ann相关的详细信息,但当我更新一条线路时,我仍会收到相同的警告。。。有什么想法吗?你是否应该使用
attr\u accessible:details\u attributes
?是的,就像我在问题中说的,我尝试了:details\u attributes和:commercial\u document\u line\u details\u attributes,这两种属性都会给我同样的错误,我不明白……如果你使用
f.simple\u fields\u for:details
,你应该收到一组不同的参数。它非常有效,我只是忘了更改它。非常感谢您抽出时间。
= f.simple_fields_for :commercial_document_line_details
= f.simple_fields_for :details
attr_accessible :commercial_document_line_details_attributes
attr_accessible :details_attributes