# Parent Child form using Partials

**URL:** <https://www.ruby-forum.com/t/parent-child-form-using-partials/63859>\
**Category:** Rails\
**Created:** [June 19, 2006, 10:56pm UTC](https://www.ruby-forum.com/t/parent-child-form-using-partials/63859 "2006-06-19T22:56:28Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![qrc](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/q/8797f3/32.png) [@qrc](https://www.ruby-forum.com/u/qrc)\
**Post date:** [June 19, 2006, 10:56pm UTC](https://www.ruby-forum.com/t/parent-child-form-using-partials/63859/1 "2006-06-19T22:56:28Z")

</div>

I am trying to make a simple application which consists of contacts and  
their addresses.

class Contact \< ActiveRecord::Base  
has\_many :addresses  
end

class Address \< ActiveRecord::Base  
belongs\_to :contact  
end

I would like to be able to edit/add addresses from within the edit/add  
of the contact. I scaffolded the contact and address objects and  
modified the contacts/\_form partial as follows:

\<%= error\_messages\_for ‘contact’ %\>

Name  
\<%= text\_field 'contact', 'name' %\>

\<%= render :partial =\> 'addresses/form', :collection =\> @contact.addresses %\>

The addresses/\_form partial looks like this:

* * *
\<%= error\_messages\_for 'address' %\>

Line 1  
\<%= text\_field 'address', 'line\_1' %\>

Line 2  
\<%= text\_field 'address', 'line\_2' %\>

City  
\<%= text\_field 'address', 'city' %\>

State  
\<%= text\_field 'address', 'state' %\>

Zip  
\<%= text\_field 'address', 'zip' %\>

I am running into to issues with this design. 1) I’ve manually added  
address objects into the table and when i go to addresses/edit/1, it  
renders the two addresses but the fields are blank. Like it sees the  
records, but its not getting put into the partial. 2) After I get the  
addresses displayed, how would I go about saving the contact and all of  
the addresses in a single post-back?

I’ve been googling for days, and it seems this should be a simple thing  
that has been encountered before.

Thanks in advance for your help!

---

<div class="post-metadata">

**Author:** ![qrc](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/q/8797f3/32.png) [@qrc](https://www.ruby-forum.com/u/qrc)\
**Post date:** [June 19, 2006, 11:11pm UTC](https://www.ruby-forum.com/t/parent-child-form-using-partials/63859/2 "2006-06-19T23:11:09Z")

</div>

Josh Huckabee wrote:

Additionally, in my contacts\_controller.rb ‘edit’ method, I have the  
following to include the address information

def edit  
@contact = Contact.find(params[:id], :include =\> :addresses)  
end

---

<div class="post-metadata">

**Author:** ![qrc](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/q/8797f3/32.png) [@qrc](https://www.ruby-forum.com/u/qrc)\
**Post date:** [June 20, 2006, 9:27pm UTC](https://www.ruby-forum.com/t/parent-child-form-using-partials/63859/3 "2006-06-20T21:27:41Z")

</div>

Anyone trying to accomplish this should check out the Sub-List form  
plugin (link above). Solves my issue and works great!

---

<div class="post-metadata">

**Author:** ![qrc](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/q/8797f3/32.png) [@qrc](https://www.ruby-forum.com/u/qrc)\
**Post date:** [June 20, 2006, 7:32pm UTC](https://www.ruby-forum.com/t/parent-child-form-using-partials/63859/4 "2006-06-20T19:32:09Z")

</div>

In case someone else stumbles along this post, I have found a plugin and  
a generator that might fit my needs. Since RubyForge is down, I haven’t  
been able to download and install. I will update with what I find.

Sub-List Form Plugin:  
[http://wiki.rubyonrails.com/rails/pages/Sub-List+Form+Plugin](http://wiki.rubyonrails.com/rails/pages/Sub-List+Form+Plugin)

Ajax Scaffold Generator:  
[http://www.ajaxscaffold.com/](http://www.ajaxscaffold.com/)
