I have:
class Company < ActiveRecord::Base
has_many :categorizations
has_many :categories, :through => :categorizations
has_many :managements
has_many :managers, :through => :managements
has_many :tenders
has_many :documents, :dependent => :destroy
accepts_nested_attributes_for :managers
accepts_nested_attributes_for :documents
accepts_nested_attributes_for :categorizations
then the form is:
= semantic_form_for @company do |f|
= f.input :name
= f.semantic_fields_for :manager do |manager|
= manager.inputs :name, :fiscal_code, :city, :zip_code, :address,
:street_number, :tel, :email
when I submit the form raise the error:
unknown attribute: manager
{“utf8”=>“✓”,
“authenticity_token”=>“V5GsRKLK8cRrGuUh2Jv4DB9wSYWG8ASCxHkd0Ur3o8s=”,
“company”=>{“manager”=>{“name”=>“”,
“fiscal_code”=>“”,
“city”=>“”,
“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:
http://api.rubyonrails.org/classes/ActiveRecord/NestedAttributes/ClassMethods.html
Which shows as example:
class Member < ActiveRecord::Base
has_many :posts
accepts_nested_attributes_for :posts
end
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