On Tue, Jan 10, 2012 at 11:33 AM, Mauro [email protected] wrote:
accepts_nested_attributes_for :managers
“zip_code”=>“”,
“address”=>“”,
“street_number”=>“”,
“tel”=>“”,
“email”=>“”}},
“commit”=>“Create Company”}
I think there is nothing wrong but perhaps I miss something.
If I am not mistaken, accepts_nested_attributes_for :managers
adds a setter method of the style company.managers_attributes=
I think your hash that is coming in from the Submit tries to do
company.manager= {“name”
=>“”,
“fiscal_code”=>“”,
“city”=>“”,
“zip_code”=>“”,
“address”=>“”,
“street_number”=>“”,
“tel”=>“”,
“email”=>“”}
and Rails complains that ‘manager’ is not an attribute of company
(really saying that the Company#manager= methods is not defined).
To validate this hypothesis, try something like, e.g. in rails console
Company.new.methods.grep(/manager/)
and check which exact methods are available for ‘manager*’ on the
Company
class.
I would expect you see there:
[… :managers_attributes=, …]
I think (not sure) a second problem is that the has_many requires an
array
there …
So, you should change your form to create parameters like this:
“company”=>{“managers_attributes”=>[ # key changed; changed into array
{“name”=>“P”,
“fiscal_code”=>“1”,
“city”=>“2560”,
…},
{“name”=>“S”,
“fiscal_code”=>“2”,
“city”=>“2500”,
…}]},
…
Also see:
Which shows as example:
class Member < ActiveRecord::Base
has_many :posts
accepts_nested_attributes_for :postsend
You can now set or update attributes on an associated post model through
the attribute hash.
For each hash that does not have an id key a new record will be
instantiated, unless the hash also contains a _destroy key that
evaluates
to true.
params = { :member => {
:name => ‘joe’, :posts_attributes => [
{ :title => ‘Kari, the awesome Ruby documentation browser!’ },
{ :title => ‘The egalitarian assumption of the modern citizen’ },
{ :title => ‘’, :_destroy => ‘1’ } # this will be ignored
]
}}
Not entirely sure, but I think this is your problem.
Peter
–
Peter V.
http://twitter.com/peter_v
http://rails.vandenabeele.com