Matt Ramos
2006-Mar-04 02:57 UTC
[Rails] Submitting data to two different tables with two different models
I''m very new to Rails. I''ve been reading Agile Web Dev for a while and read up on ruby, but I''m still learning. I designed a website for someone for an engineering shop. New projects had to be added manually. Well, I figured this would be a great canidate for railifying! I have the form working perfectly, you can add your data and get it in a table, and listing projects works, too. I''m working on a photo gallery now, and I have a lot of it working. I took it from the agile web dev book. I''d like to know how to join the two forms and submit it as one. I''m not sure how to submit data to two different places via one form. Suggestions and links are greatly appreciated. Thanks! -- -Matt -------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060304/a31e8dcc/attachment.html
Norman Timmler
2006-Mar-05 12:57 UTC
[Rails] Submitting data to two different tables with two different models
Am Freitag, den 03.03.2006, 21:57 -0500 schrieb Matt Ramos:> I''m very new to Rails. I''ve been reading Agile Web Dev for a while and > read up on ruby, but I''m still learning. > > I designed a website for someone for an engineering shop. New projects > had to be added manually. > > Well, I figured this would be a great canidate for railifying! I have > the form working perfectly, you can add your data and get it in a > table, and listing projects works, too. I''m working on a photo gallery > now, and I have a lot of it working. I took it from the agile web dev > book. I''d like to know how to join the two forms and submit it as > one. > > I''m not sure how to submit data to two different places via one form. > Suggestions and links are greatly appreciated. Thanks!Take two text fields for example: # view <%= form_tag :controller => :posts, :action => create %> <%= text_field("post", "title", "size" => 20) %> <%= text_field("user", "email", "size" => 20) %> <%= end_form_tag %> # posts_controller class PostsController < ApplicationController def create @post = Post.create(params[:post]) @user = User.create(params[:user]) @post.users << @user end end In the controller the params hash has one key for every object (user, post) in your form. As a values of this key you find a hash having a key-value pair for every object attribute (title, email). This way you can submit multiple objects via one form. -- Norman Timmler http://blog.inlet-media.de
Michael Gorsuch
2006-Mar-05 13:05 UTC
[Rails] Submitting data to two different tables with two different models
Norman - wouldn''t you also have to call a "@post.save" at the end to save the relationship? On 3/5/06, Norman Timmler <lists@inlet-media.de> wrote:> Am Freitag, den 03.03.2006, 21:57 -0500 schrieb Matt Ramos: > > I''m very new to Rails. I''ve been reading Agile Web Dev for a while and > > read up on ruby, but I''m still learning. > > > > I designed a website for someone for an engineering shop. New projects > > had to be added manually. > > > > Well, I figured this would be a great canidate for railifying! I have > > the form working perfectly, you can add your data and get it in a > > table, and listing projects works, too. I''m working on a photo gallery > > now, and I have a lot of it working. I took it from the agile web dev > > book. I''d like to know how to join the two forms and submit it as > > one. > > > > I''m not sure how to submit data to two different places via one form. > > Suggestions and links are greatly appreciated. Thanks! > > Take two text fields for example: > > # view > <%= form_tag :controller => :posts, :action => create %> > <%= text_field("post", "title", "size" => 20) %> > <%= text_field("user", "email", "size" => 20) %> > <%= end_form_tag %> > > # posts_controller > class PostsController < ApplicationController > def create > @post = Post.create(params[:post]) > @user = User.create(params[:user]) > @post.users << @user > end > end > > In the controller the params hash has one key for every object (user, > post) in your form. As a values of this key you find a hash having a > key-value pair for every object attribute (title, email). > > This way you can submit multiple objects via one form. > -- > Norman Timmler > > http://blog.inlet-media.de > > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
Norman Timmler
2006-Mar-05 14:18 UTC
[Rails] Submitting data to two different tables with two different models
Am Sonntag, den 05.03.2006, 07:05 -0600 schrieb Michael Gorsuch:> Norman - wouldn''t you also have to call a "@post.save" at the end to > save the relationship? > > On 3/5/06, Norman Timmler <lists@inlet-media.de> wrote: > > # posts_controller > > class PostsController < ApplicationController > > def create > > @post = Post.create(params[:post]) > > @user = User.create(params[:user]) > > @post.users << @user > > end > > endIt depends on the kind of association type binding the two models. If User belongs_to :post and Post has_many :users, the << method will call save on the child object (User) to make the association permanent. The same happens to child objects at has_and_belongs_to_many and has_one associations. So @post.user = @user also needs no save on @user. In the difference (assume User belongs_to :post as above), if you do @user.post = @post, you have to do a @user.save after associating them. -- Norman Timmler http://blog.inlet-media.de
Matt Ramos
2006-Mar-08 21:07 UTC
[Rails] Submitting data to two different tables with two different models
Thanks a lot, I got it working. On 3/5/06, Norman Timmler <lists@inlet-media.de> wrote:> > Am Sonntag, den 05.03.2006, 07:05 -0600 schrieb Michael Gorsuch: > > Norman - wouldn''t you also have to call a "@post.save" at the end to > > save the relationship? > > > > On 3/5/06, Norman Timmler <lists@inlet-media.de> wrote: > > > # posts_controller > > > class PostsController < ApplicationController > > > def create > > > @post = Post.create(params[:post]) > > > @user = User.create(params[:user]) > > > @post.users << @user > > > end > > > end > > It depends on the kind of association type binding the two models. > > If User belongs_to :post and Post has_many :users, the << method will > call save on the child object (User) to make the association permanent. > The same happens to child objects at has_and_belongs_to_many and has_one > associations. So @post.user = @user also needs no save on @user. > > In the difference (assume User belongs_to :post as above), if you do > @user.post = @post, you have to do a @user.save after associating them. > -- > Norman Timmler > > http://blog.inlet-media.de > > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-- -Matt -------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060308/d95de8be/attachment.html