Forum: Ruby on Rails NOOB: Representing linked objects in one form

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
donncha (Guest)
on 2006-05-17 16:57
Given two models:

  :address_id    # foreign key



I want to have a form allowing a user to register, in which she'd enter
an address as well, but how do I go about combining both objects into
the one form?

I'm new to this and following along with the Agile Rails book from the
Pragmatic Programmers, but it doesn't mention this and I'm having
trouble getting a decent answer with searches.
Thibaut B. (Guest)
on 2006-05-17 17:40
(Received via mailing list)
have a look over there :

Stephen B. (Guest)
on 2006-05-17 17:56
(Received via mailing list)
I'm not sure that it's ajax that's needed here as such.

The address fields on the form will look something like this:
<input type="text" name="address[line_1]"/>
<input type="text" name="address[line_2]"/>
<input type="text" name="address[city]"/>

You should then be able to create a new address via the user object in
your controller:

def register_user
   user =[:user])

I might be wrong but i would think that you need to have a user_id in
'addresses' rather than an address_id in 'users' - the above assumes
that this is the case. It also assumes that users have many addresses.

Hope this helps,

Adam B. (Guest)
on 2006-05-17 20:01
To keep with the Rails style, the text fields should look like this:

<%= text_field 'address', 'line_1' %>
<%= text_field 'address', 'line_2' %>
<%= text_field 'address', 'city' %>

and Steve is right: if a user has_one or has_many addresses, the foriegn
id field should be in the address table.

donncha (Guest)
on 2006-05-18 03:47
Oops. Looks like I've more fundamental problems to be worrrying about.
This topic is locked and can not be replied to.