Parent Child together in input form

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

Parent Child together in input form

Harm de Laat-2
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 and Address can be filled in?

Regards,

Harm de Laat


_______________________________________________
Rails mailing list
[hidden email]
http://lists.rubyonrails.org/mailman/listinfo/rails
Reply | Threaded
Open this post in threaded view
|

Re: Parent Child together in input form

Alain Ravet-2
Harm

Check the thread
    "managing belongs_to fields in a form"

I posted this very question a few hours ago.

Alain

--
Posted via http://www.ruby-forum.com/.
_______________________________________________
Rails mailing list
[hidden email]
http://lists.rubyonrails.org/mailman/listinfo/rails
Reply | Threaded
Open this post in threaded view
|

Re: Parent Child together in input form

Alain Ravet-2
Harm,

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

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

      customer = Customer.new
      address = Address.new

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



Alain

--
Posted via http://www.ruby-forum.com/.
_______________________________________________
Rails mailing list
[hidden email]
http://lists.rubyonrails.org/mailman/listinfo/rails
Reply | Threaded
Open this post in threaded view
|

Re: Re: Parent Child together in input form

Harm de Laat-2
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}"/></input>

What is the equivalent of the above in RHTML?

Regards,

Harm.

On 12/16/05, Alain Ravet <[hidden email]> wrote:
Harm,

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

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

      customer = Customer.new
      address = Address.new

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



Alain

--
Posted via http://www.ruby-forum.com/.
_______________________________________________
Rails mailing list
[hidden email]
http://lists.rubyonrails.org/mailman/listinfo/rails


_______________________________________________
Rails mailing list
[hidden email]
http://lists.rubyonrails.org/mailman/listinfo/rails
Reply | Threaded
Open this post in threaded view
|

Re: Re: Parent Child together in input form

Alain Ravet-2
Harn,

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

  def new
    @customer = Customer.new
    @address  = Address.new
  end

  def create
    @address = Address.new(params[:address])
    @customer = Customer.new(params[:customer])
    @customer.address = @address

    if @address.save && @customer.save
      flash[:notice] = 'Member was successfully created.'
      redirect_to :action => 'list'
    else
      render :action => 'new'
    end
  end


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


Alain

--
Posted via http://www.ruby-forum.com/.
_______________________________________________
Rails mailing list
[hidden email]
http://lists.rubyonrails.org/mailman/listinfo/rails