変!ネストされた属性フィールドを宣言するために使用fields_for
すると、railsはネストされた属性のIDを持つ非表示の属性を追加します(更新を実行するため)。
= form_for @opinion do |f|
= f.fields_for :client do |client_f|
= client_f.text_field :name
私に与える:
<input name="opinion[client_attributes][name]" type="text" />
<input name="opinion[client_attributes][id]" type="hidden" value="4" />
これはにつながります:
Can't mass-assign protected attributes: client_attributes
もちろん、ここに私のモデルがあります:
class Opinion < ActiveRecord::Base
attr_accessible :content
attr_accessible :client_id
validates :content, :presence => true, :length => { :maximum => 2048 }
belongs_to :client
accepts_nested_attributes_for :client
end
class Client < ActiveRecord::Base
attr_accessible :name
validates :name, :presence => true, :length => { :maximum => 64 }
has_many :opinions
end
レールビューに問題がありますか、それともモデルに問題がありますか?
それを修正する方法はありますか?前もって感謝します。