Reputation: 7654
Strange! When I use fields_for
to declare a nested attribute field, rails adds a hidden attribute with the id of the nested attribute (to perform an update):
= form_for @opinion do |f|
= f.fields_for :client do |client_f|
= client_f.text_field :name
Gives me:
<input name="opinion[client_attributes][name]" type="text" />
<input name="opinion[client_attributes][id]" type="hidden" value="4" />
This leads to:
Can't mass-assign protected attributes: client_attributes
Of course, here are my models :
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
Is is a problem with the rails view, or a model problem ?
Any idea how to fix that? Thanks in advance.
Upvotes: 1
Views: 832
Reputation: 4636
Add :client_attributes
into :attr_accessible
:attr_accessible
is used to identify which fields are opened for mass-assignment.
In the request you send to controller, there should be a parameter key called client_attributes
to group the details of clients. You must make this one enable for mass-assignment so you can put the details of clients to update in the way of mass-assignment.
Upvotes: 4