I have a parent which has multiple children. I want it so that when I submit my form, a parent is generated in the parent model, and multiple records are created in the child model, one for each child. When I try and submit, I get the following error:
ActiveRecord::AssociationTypeMismatch in ParentsController#create
Child(#) expected, got Array(#)
When I uncomment accepts_nested_attributes_for :children and change f.fields_for :children to f.fields_for :children_attributes, I get a different error:
TypeError in ParentsController#create
can't convert Symbol into Integer
I am at a loss as to what to do. I have checked out the nested model forms railscasts, but they were dealing with generating children fields inside the form, and what I learned from the railscasts didn't seem to work. I am pretty I am doing my builder.text_field :cname's in my form wrong, but I'm not aware of the proper way to do it.
My code:
parent.rb
class Parent < ActiveRecord::Base
has_many :children
#accepts_nested_attributes_for :children
attr_protected :id
child.rb
class Child < ActiveRecord::Base
belongs_to :parent
attr_protected :id
_form.html.erb
<%= form_for @parent, :url => { :action => "create" } do |f| %>
<%= f.text_field :pname %>
<%= f.fields_for :children do |builder| %>
<%= builder.text_field :cname %>
<%= builder.text_field :cname %>
<%= builder.text_field :cname %>
<% end %>
<%= f.submit %>
<% end %>
params content:
{"utf8"=>"✓",
"authenticity_token"=>"FQQ1KdNnxLXolfes9IGiO+aKHJaPCH+2ltDdA0TwF7w=",
"parent"=>{"pname"=>"Heman",
"child"=>{"cname"=>""}},
"commit"=>"Create"}