How do you create multiple objects with one form? For example: <% form_for :user, :url => users_path, :html => { :method => :post } do |f| %> <table> <tr> <th>Name:</th> <td><label for="user_name"><%= f.text_field :name %></label></td> <th>Number:</th> <td><label for="user_number"><%= f.text_field :number %></label></td> <th>Name:</th> <td><label for="user_name"><%= f.text_field :name %></label></td> <th>Number:</th> <td><label for="user_number"><%= f.text_field :number %></label></td> </tr> </table> <p><%= submit_tag "Create Users" %></p> --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Surely this must be common ... --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
For multiple objects of the same type just add a :index argument to each of the form fields. Here is an example: <%= f.select :rating, @ratings, {}, {:index => post.id} %> For existing objects you can use the objects id for an index, for new objects just use some kind of counter. When you submit the form the index objects will be rolled up into a hash that you can then step through and process. Aaron --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Thanks for the reply. I don''t get what you mean by using some kind of counter. I''ve taken a look at this post but don''t think it suits my needs: http://groups.google.com/group/rubyonrails-talk/browse_thread/thread/10ab20cf27de5725/1d739f6058c9f4ba?lnk=gst&q=multiple+object&rnum=6#1d739f6058c9f4ba I''ve got all the rails books and cannot find some documentation on how to do something like this. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
The :index value needs to be unique for each object. If he objects already exist the id works great, otherwise you need to create a unique key for each object. The code below uses the counter loop variable for the :index value. # wrap everything in a form_for <%= form_for ........ %> # loop to create multiple objects <% 3.times do |counter| %> # setup fields for each object <% fields_for posts, post do |post_fields| %> # use the counter variable for the :index key <%= post_fields.select :rating, @ratings, {}, {:index => counter} %> <%= post_fields.text_field :name, :index => counter %> <% end %> <% end %> <% end %> Aaron --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Thanks for the reply. How would one implement this if the number of objects is unknown until the post? --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
I plan on using rjs insert_html for each additional object beyond one that the user wants to create. The reason why I am doing it this way is because the objects that are created, each will have the same foreign key (mapped to a different model). If their all going to have the same foreign keys, I think it makes sense to create them all on the same form/view. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
I''m just going to create 5 unique indexed objects for the ''new'' action. Thanks for your help Aaron! --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
I found some of the tutorials at railsforum.com useful for figuring out how to do this. Specifically: http://www.railsforum.com/viewtopic.php?id=719 http://www.railsforum.com/viewtopic.php?id=1063 http://www.railsforum.com/viewtopic.php?id=1065 Michael www.mslater.com On Feb 26, 3:06 am, "jko170" <jko...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> How do you create multiple objects with one form? For example: > > <% form_for :user, :url => users_path, :html => { :method => :post } > do |f| %> > <table> > <tr> > <th>Name:</th> > <td><label for="user_name"><%= f.text_field :name %></label></td> > <th>Number:</th> > <td><label for="user_number"><%= f.text_field :number %></label></td> > <th>Name:</th> > <td><label for="user_name"><%= f.text_field :name %></label></td> > <th>Number:</th> > <td><label for="user_number"><%= f.text_field :number %></label></td> > </tr> > </table> > > <p><%= submit_tag "Create Users" %></p>--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---