Forum: Ruby on Rails 1 Form for Model containing another Model??

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
3cbe6157fca13c0fbbb5ed81b803c555?d=identicon&s=25 JW (Guest)
on 2006-05-19 18:35
Hi,

I can have a Person model with a "has_one :address_email" association,
then do the following in my controller:

@person = Person.new
@person.address_email = AddressEmail.new

But, when I have 1 form for "Person" in my view I can't bind fields in
the @person.address_email object with text_field or text_field_tag
helpers without getting a "undefined local variable or method.  The
helpers don't seem to be able to access the composed/attribute model no
matter what syntax I use.

I _can_ query the @person.address_email object via a breakpoint console.

Page 343 of the Agile book seems to indicate what I'm after is possible:
"Form Parameters: user[address][city] = Wien , params: { :user => {
:address => { :city => "Wien" }}}"

So any ideas?  Is it possible to have 1 form with two models where one
model is an instance variable of the other?

Thanks much for any thoughts!

Cheers, JW
C64e63b70be7dfed8b0742540b8b27e5?d=identicon&s=25 Mark Reginald James (Guest)
on 2006-05-20 13:00
(Received via mailing list)
JW wrote:
> matter what syntax I use.
No you can't do this, though a core patch is available that makes it
possible: <http://dev.rubyonrails.org/ticket/2053>

Instead I'd suggest using something like:

@address_email = @person.build_address_email(params[:address_email])

text_field :address_email, :address1
text_field :address_email, :email

--
We develop, watch us RoR, in numbers too big to ignore.
3cbe6157fca13c0fbbb5ed81b803c555?d=identicon&s=25 JW (Guest)
on 2006-06-01 00:53
Mark Reginald James wrote:
> JW wrote:
>> matter what syntax I use.
> No you can't do this, though a core patch is available that makes it
> possible: <http://dev.rubyonrails.org/ticket/2053>
>
> Instead I'd suggest using something like:
>
> @address_email = @person.build_address_email(params[:address_email])
>
> text_field :address_email, :address1
> text_field :address_email, :email

Hi Mark,

First thanks very much for your help and comment... I was really
stumbling hard on that one.

Will examine the build_address_email method in ActiveRecord but do hope
the core patch makes it in as it seems to be straight-forward.

Cheers and thanks again...

JW
This topic is locked and can not be replied to.