Given two models: User :name :email :address_id # foreign key Address :line_1 :line_2 :city etc. I want to have a form allowing a user to register, in which she''d enter an address as well, but how do I go about combining both objects into the one form? I''m new to this and following along with the Agile Rails book from the Pragmatic Programmers, but it doesn''t mention this and I''m having trouble getting a decent answer with searches. -- Posted via http://www.ruby-forum.com/.
Thibaut Barrère
2006-May-17 13:40 UTC
[Rails] NOOB: Representing linked objects in one form
have a look over there : www.ajaxscaffold.com Thibaut -------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060517/5ac5980e/attachment.html
Stephen Bartholomew
2006-May-17 13:53 UTC
[Rails] NOOB: Representing linked objects in one form
I''m not sure that it''s ajax that''s needed here as such. The address fields on the form will look something like this: <input type="text" name="address[line_1]"/> <input type="text" name="address[line_2]"/> <input type="text" name="address[city]"/> You should then be able to create a new address via the user object in your controller: def register_user user = User.new(params[:user]) if user.save user.addresses.create(params[:address]) end end I might be wrong but i would think that you need to have a user_id in ''addresses'' rather than an address_id in ''users'' - the above assumes that this is the case. It also assumes that users have many addresses. Hope this helps, Steve Thibaut Barr?re wrote:> have a look over there : www.ajaxscaffold.com <http://www.ajaxscaffold.com> > > Thibaut > > > > ------------------------------------------------------------------------ > > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails > > > ------------------------------------------------------------------------ > > No virus found in this incoming message. > Checked by AVG Free Edition. > Version: 7.1.392 / Virus Database: 268.6.0/341 - Release Date: 16/05/2006
Adam Bloom
2006-May-17 16:02 UTC
[Rails] Re: NOOB: Representing linked objects in one form
To keep with the Rails style, the text fields should look like this: <%= text_field ''address'', ''line_1'' %> <%= text_field ''address'', ''line_2'' %> <%= text_field ''address'', ''city'' %> and Steve is right: if a user has_one or has_many addresses, the foriegn id field should be in the address table. -Adam -- Posted via http://www.ruby-forum.com/.