Abdullah Esmail
2013-Jan-30 09:34 UTC
Multiple forms to create multiple records for multiple users
Hello. I have come across a scenario where I need to save multiple records for multiple users on the same page. User has_many :shifts Shift belongs_to :user I need to show multiple shifts for each user on the same page and the administrator will just enter some values and click save. Example: user1: <shift1_value> - <shift2_value> - <shift3_value> user2: <shift1_value> - <shift2_value> - <shift3_value> user3: <shift1_value> - <shift2_value> - <shift3_value> [save] What''s the best way to tackle this problem in rails? Thank you. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/72HFVK-JilIJ. For more options, visit https://groups.google.com/groups/opt_out.
Jim Ruther Nill
2013-Jan-30 14:34 UTC
Re: Multiple forms to create multiple records for multiple users
On Wed, Jan 30, 2013 at 5:34 PM, Abdullah Esmail <abdullah.esmail-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>wrote:> Hello. > I have come across a scenario where I need to save multiple records for > multiple users on the same page. > > User has_many :shifts > Shift belongs_to :user > > I need to show multiple shifts for each user on the same page and the > administrator will just enter some values and click save. > > Example: > user1: <shift1_value> - <shift2_value> - <shift3_value> > user2: <shift1_value> - <shift2_value> - <shift3_value> > user3: <shift1_value> - <shift2_value> - <shift3_value> > [save] > > What''s the best way to tackle this problem in rails? >This all depends on how you setup the form in your views. I suggest you create a spectific action, outside of the 7 crud actions, to handle the data that you get when you do this. As for the form, I think the names of the elements should be shifts[][user_id] for the user_id shifts[][shifts][] for the 3 shift values you''ll receive a hash that looks like shifts: [{ user_id: 1, shifts: [1,2,3] }, { user_id: 2, shifts: [1,2,3] }] on the controller. good luck!> > Thank you. > > -- > You received this message because you are subscribed to the Google Groups > "Ruby on Rails: Talk" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To view this discussion on the web visit > https://groups.google.com/d/msg/rubyonrails-talk/-/72HFVK-JilIJ. > For more options, visit https://groups.google.com/groups/opt_out. > > >-- ------------------------------------------------------------- visit my blog at http://jimlabs.heroku.com -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit https://groups.google.com/groups/opt_out.