Forum: Ruby on Rails Creating multiple objects with one form

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
jko170 (Guest)
on 2007-02-26 13:08
(Received via mailing list)
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>
jko170 (Guest)
on 2007-02-26 22:13
(Received via mailing list)
Surely this must be common ...
Aaron (Guest)
on 2007-02-27 00:00
(Received via mailing list)
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
jko170 (Guest)
on 2007-02-27 01:45
(Received via mailing list)
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/br...

I've got all the rails books and cannot find some documentation on how
to do something like this.
Aaron (Guest)
on 2007-02-27 02:14
(Received via mailing list)
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
jko170 (Guest)
on 2007-02-27 02:22
(Received via mailing list)
Thanks for the reply. How would one implement this if the number of
objects is unknown until the post?
jko170 (Guest)
on 2007-02-27 04:41
(Received via mailing list)
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.
jko170 (Guest)
on 2007-02-27 06:38
(Received via mailing list)
I'm just going to create 5 unique indexed objects for the 'new'
action. Thanks for your help Aaron!
Michael S. (Guest)
on 2007-02-28 04:11
(Received via mailing list)
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
This topic is locked and can not be replied to.