Forum: Ruby on Rails Parent Child together in input 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.
Harm de Laat (Guest)
on 2005-12-16 15:05
(Received via mailing list)
Hi all,

I understand how rails is supposed to work when dealing with one entity
input form.

However, when I have a relation between two objects (composition):

Customer(1)  --------> (1)Address

How can you create a inputform where both the properties of a Customer
Address can be filled in?


Harm de Laat
Alain R. (Guest)
on 2005-12-16 15:08

Check the thread
    "managing belongs_to fields in a form"

I posted this very question a few hours ago.

Alain R. (Guest)
on 2005-12-16 15:14

Sorry, I misread your post. Your question is different.

I would  simply input the 2 objects independantly in the same form

      customer =
      address =

, build them separately from their parts, link the address to the
customer and finally save them both.

Harm de Laat (Guest)
on 2005-12-16 15:44
(Received via mailing list)
Thx for the reply!

Do you maybe have an example of this?

How do I do this in de RHTML file?

In JSP i might be able to do something like:

<input type="text" name="<%= <c:out value="${customer.address.street

What is the equivalent of the above in RHTML?


Alain R. (Guest)
on 2005-12-16 15:59

You can start from the default scaffold code, and simply extend it:
(caution: untested advice, suggested by a newbie !!)

  def new
    @customer =
    @address  =

  def create
    @address =[:address])
    @customer =[:customer])
    @customer.address = @address

    if &&
      flash[:notice] = 'Member was successfully created.'
      redirect_to :action => 'list'
      render :action => 'new'

For the rhtml, update the view (the _form.rhtml partial) accordingly.

This topic is locked and can not be replied to.