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' %>
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!