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'' %>
<!--[form:contact]-->
<p><label for="contact_name">Name</label><br/>
<%= text_field ''contact'', ''name''
%></p>
<%= render :partial => ''addresses/form'', :collection
=>
@contact.addresses %>
<!--[eoform:contact]-->
The addresses/_form partial looks like this:
<hr />
<%= error_messages_for ''address'' %>
<!--[form:address]-->
<p><label for="address_line_1">Line
1</label><br/>
<%= text_field ''address'', ''line_1''
%></p>
<p><label for="address_line_2">Line
2</label><br/>
<%= text_field ''address'', ''line_2''
%></p>
<p><label for="address_city">City</label><br/>
<%= text_field ''address'', ''city''
%></p>
<p><label
for="address_state">State</label><br/>
<%= text_field ''address'', ''state''
%></p>
<p><label for="address_zip">Zip</label><br/>
<%= text_field ''address'', ''zip''
%></p>
<!--[eoform:address]-->
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!
--
Posted via http://www.ruby-forum.com/.