0

多くの人がすでに同じ問題を抱えていることは知っていますが、私の特定のケースの解決策は見つかりませんでした。私は次のモデルを持っています:

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

class Invoicing::CommercialDocumentLineDetail < ActiveRecord::Base
  attr_accessible :description, :commercial_document_line_id

  belongs_to :commercial_document_line
end

CommercialDocumentLineユーザーが詳細を追加できるようにする更新用のフォームがあります。

= simple_form_for line do |f|

  ## some code here which works great

  = f.simple_fields_for :commercial_document_line_details do |fd|
    = fd.input :description

問題は、私が警告を出し続けることです:

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

= f.simple_fields_for :details

その場合、フォームは表示されません。そして私が交換した場合

attr_accessible :commercial_document_line_details_attributes

attr_accessible :details_attributes

その後、同じエラーが発生します。

私は確かにここで何かが欠けていますが、それを理解することはできません。
どんな助けでもいただければ幸いです。

4

1 に答える 1

3

を使用する= f.simple_fields_for :detailsと、実際にはの詳細のリストが取得されf.objectます。新しいオブジェクトの場合、これは通常空白であるため、何も表示されません。これが正しい使用方法fields_forです。を使用してコントローラで、@line.details.buildまたはを使用してビューで詳細を作成する必要があります。f.object.details.build

于 2013-02-04T11:17:29.480 に答える