# Multiple objects from an add form

**URL:** <https://www.ruby-forum.com/t/multiple-objects-from-an-add-form/72954>\
**Category:** Rails\
**Created:** [October 20, 2006, 2:15am UTC](https://www.ruby-forum.com/t/multiple-objects-from-an-add-form/72954 "2006-10-20T02:15:00Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![dsl239](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/3bc359/32.png) [@dsl239](https://www.ruby-forum.com/u/dsl239)\
**Post date:** [October 20, 2006, 2:15am UTC](https://www.ruby-forum.com/t/multiple-objects-from-an-add-form/72954/1 "2006-10-20T02:15:00Z")

</div>

Hi,

I am trying to create multiple objects from the same “add” form

For example:

Say I have a page where I want to add two movies, I have currently tried

The problem comes in the controller. The keys (0 and 1) are passed as  
Symbols and I get an error saying

“Symbol as array index”

when I try

for movie in @params[:movie]  
Movie.create(movie)  
end

I’m not sure why this doesn’t work and it would seem that there has to  
be an easy way to do it. Any help will be appreciated

Thanks

---

<div class="post-metadata">

**Author:** ![dsl239](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/3bc359/32.png) [@dsl239](https://www.ruby-forum.com/u/dsl239)\
**Post date:** [October 20, 2006, 2:54am UTC](https://www.ruby-forum.com/t/multiple-objects-from-an-add-form/72954/2 "2006-10-20T02:54:18Z")

</div>

Dan Langevin wrote:

> The problem comes in the controller. The keys (0 and 1) are passed as  
> Symbols and I get an error saying
> 
> “Symbol as array index”
> 
> when I try
> 
> for movie in @params[:movie]  
> Movie.create(movie)  
> end

Try: for movie in params[:movie].values

–  
We develop, watch us RoR, in numbers too big to ignore.

---

<div class="post-metadata">

**Author:** ![dsl239](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/3bc359/32.png) [@dsl239](https://www.ruby-forum.com/u/dsl239)\
**Post date:** [October 20, 2006, 4:00pm UTC](https://www.ruby-forum.com/t/multiple-objects-from-an-add-form/72954/3 "2006-10-20T16:00:28Z")

</div>

Dan,

Have you tried using a \<%= text\_field(“movie[]”, “name”) %\> tag?  
(Notice the square brackets after the object name). The resulting  
html, when submitted, maps to a params object that contains a hash of  
hashes. You can then use the following code to do an update:

Movie.update(params[:movie].keys, params[:movie].values)

If you own the AWDWR book, it’s documented in a sidebar called “Forms  
Containing Collections” in chapter 17.

Hope it helps,

-Anthony

---

<div class="post-metadata">

**Author:** ![dsl239](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/3bc359/32.png) [@dsl239](https://www.ruby-forum.com/u/dsl239)\
**Post date:** [October 20, 2006, 6:48pm UTC](https://www.ruby-forum.com/t/multiple-objects-from-an-add-form/72954/4 "2006-10-20T18:48:41Z")

</div>

Anthony C. wrote:

> Dan,
> 
> Have you tried using a \<%= text\_field(“movie[]”, “name”) %\> tag?  
> (Notice the square brackets after the object name). The resulting  
> html, when submitted, maps to a params object that contains a hash of  
> hashes. You can then use the following code to do an update:
> 
> Movie.update(params[:movie].keys, params[:movie].values)
> 
> If you own the AWDWR book, it’s documented in a sidebar called “Forms  
> Containing Collections” in chapter 17.
> 
> Hope it helps,
> 
> -Anthony

Thanks guys, the first solution worked. The second would work if I were  
updating (I saw it in the book as well) but since I’m creating new  
records, I wasn’t sure how to do it.

Thanks again,

Dan
