1 Form for Model containing another Model?


#1

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


#2

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.


#3

Mark Reginald J. 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