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 <input type="text" name="movie[0][name]"> <input type="text" name="movie[1][name]"> 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 -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Dan Langevin wrote:> 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 > > <input type="text" name="movie[0][name]"> > > <input type="text" name="movie[1][name]"> > > 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) > endTry: for movie in params[:movie].values -- We develop, watch us RoR, in numbers too big to ignore. --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
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 On Oct 19, 2006, at 8:53 PM, Mark Reginald James wrote:> > Dan Langevin wrote: > >> 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 >> >> <input type="text" name="movie[0][name]"> >> >> <input type="text" name="movie[1][name]"> >> >> 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. > > >--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Anthony Carlos 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, > > -AnthonyThanks 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 -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---