Multiple objects from an add form

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

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.

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

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