Anthony Carlos
2006-Nov-04 02:36 UTC
[Rails] Using Field Helpers to Edit a Related Model Object
Hello folks,
Here''s a question about creating an edit screen for a model and a
related model. On page 355 of the AWDWR book (printed page 464 of the
2nd edition pdf), there''s a little table showing how form parameters
map to the params object. One of them is:
user[address][city]=Wien
... maps to params as ...
{ :user => { :address => { :city => ''Wien'' }}}
I completely understand why this is happening. The user object
has_one address object which has an attribute called city.
My question is related to the idea that this alludes to being able to
edit a user object and a related address object in the same action.
Later on, the book says we can use the following code for an easy way
to update the object. It doesn''t say whether or not the related
object get saved.
user.update_attributes(params[:user])
I wanted to test whether the related object was saved, but I cannot
find any documentation on how to create the text field that will
produce the proper html code.
I''ve tried:
<%= text_field :user "address.city" > => undefined method
address.city for User
<%= text_field :user "address.city" > => undefined method
address
[''city''] for User
So, what''s the proper syntax for the text_field helper to access a
related model object? Or, am I going down the wrong path and the
update won''t update both model objects anyway?
Thanks for the help,
-Anthony
