# Create multiple objects in 1 form

**URL:** <https://www.ruby-forum.com/t/create-multiple-objects-in-1-form/178586>\
**Category:** Rails\
**Created:** [November 7, 2009, 5:34am UTC](https://www.ruby-forum.com/t/create-multiple-objects-in-1-form/178586 "2009-11-07T05:34:42Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![eggie5](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/7c8e57/32.png) [@eggie5](https://www.ruby-forum.com/u/eggie5)\
**Post date:** [November 7, 2009, 5:34am UTC](https://www.ruby-forum.com/t/create-multiple-objects-in-1-form/178586/1 "2009-11-07T05:34:42Z")

</div>

I have an model in my rails app called Dog. I want to create a form  
where I can create an arbitrary amount of dogs with one request.

Here’s the default for creating a dog form:

# New dog

\<% form\_for(@dog) do |f| %\>  
\<%= f.error\_messages %\>  
\<%= f.label :confirmations %\>  
  
\<%= f.text\_field :confirmations %\>

\<%= f.label :name%\>  
 \<%= f.text\_field :name%\>

\<%= f.submit 'Create' %\>

\<% end %\>

with this controller code:

def new  
@dog= Dog.new

```
respond_to do |format|
  format.html # new.html.erb
  format.xml { render :xml => @dog}
end

```

end

def create  
@dog= Dog.new(params[:dog])

```
respond_to do |format|
  if @ dog.save
    flash[:notice] = 'dog was successfully created.'
    format.html { redirect_to(@ dog) }
    format.xml { render :xml => @ dog, :status

```

=\> :created, :location =\> @ dog}  
else  
format.html { render :action =\> “new” }  
format.xml { render :xml =\> @ dog.errors, :status  
=\> :unprocessable\_entity }  
end  
end  
end

So here I have a form where I can create a dog in the DB. How do I  
create a form (and the respective controller code) to create 2 or 3  
dogs?

---

<div class="post-metadata">

**Author:** ![eggie5](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/7c8e57/32.png) [@eggie5](https://www.ruby-forum.com/u/eggie5)\
**Post date:** [November 7, 2009, 8:42am UTC](https://www.ruby-forum.com/t/create-multiple-objects-in-1-form/178586/2 "2009-11-07T08:42:32Z")

</div>

On Fri, Nov 06, 2009 at 08:33:02PM -0800, eggie5 wrote:

> So here I have a form where I can create a dog in the DB. How do I  
> create a form (and the respective controller code) to create 2 or 3  
> dogs?

While I’m admittedly a rails newcomer–and probably destined to go back  
to PHP if I can’t get some traction on some of my own forms issues–I  
can think of two practical ways to go about this:

```
1. Have a successful create add some feedback to flash[:notice] and
   redirect to another dog entry form. As long as the session (or
   some other mechanism) is populating a foreign key into each dog
   object so you can find the records again, you could do this an
   arbitrary number of times.

2. Use AJAX to get the number of dogs you want to enter, and then
   create a nested form for each one.

```

Maybe some of the more experienced rails folk will have better  
suggestions for you.

–  
“Oh, look: rocks!”  
– Doctor Who, “Destiny of the Daleks”
