Hi all, I thought I was trying to do something rather simple, but it has caused me a lot of headaches. Here it is: - in my program: model_a has_many model_b. model_b has_many model_c. - in the user interface, I would like the user to be able to fill the information for one model_a, one or more associated model_bs (corresponding fields could be added with a set of links ''add/remove a model_b''), and for each model_b added, one or more model_c (with the same mechanism). Thanks to the great tutorial "Handle multiple models in one form", it''s easy to handle the first step (any number of model_b per model_a), but I can''t get the second step to work. The natural indexing should (I think) be: "model_a[model_b_attributes][][model_c_attributes][][an_attribute]" But it doesn''t seem to work. I read here http://guides.rubyonrails.org/form_helpers.html that "only one level of “arrayness” is allowed" and that I should use hashes instead, but I am not quite sure I understand how to do it. Do you have any idea? Thanks a lot PJ --~--~---------~--~----~------------~-------~--~----~ 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@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Frederick Cheung
2009-Apr-15 08:05 UTC
Re: Complex forms: only one level of “arrayness” is allowed?
On Apr 15, 1:07 am, PJ <camillieri...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > "model_a[model_b_attributes][][model_c_attributes][][an_attribute]" > > But it doesn''t seem to work. I read herehttp://guides.rubyonrails.org/form_helpers.html > that "only one level of “arrayness” is allowed" and that I should use > hashes instead, but I am not quite sure I understand how to do it. > > Do you have any idea?What that comment is saying is that you can replace an array eg [a,b,c] with a hash eg {1 => a, 2=> b, 3=>c} Generating those keys is up to your client side code, approaches included using a number you increment or going for some unique thing you generate on the spot (eg time in milliseconds concatenated with a random number) so that you don''t have to keep track of how many fields and what not you have created clientside. Fred --~--~---------~--~----~------------~-------~--~----~ 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@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Thanks Fred. Considering there is a bit of "wizardry" that needs to be done and that Rails doesn''t include helpers to do this simply, does it mean it is not recommended to build such complex forms? I am not very experienced with Rails, but I love the fact it is very "clean" and I feel a bit cautious having to generate keys manually. PJ On Apr 15, 9:05 am, Frederick Cheung <frederick.che...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On Apr 15, 1:07 am, PJ <camillieri...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > > "model_a[model_b_attributes][][model_c_attributes][][an_attribute]" > > > But it doesn''t seem to work. I read herehttp://guides.rubyonrails.org/form_helpers.html > > that "only one level of “arrayness” is allowed" and that I should use > > hashes instead, but I am not quite sure I understand how to do it. > > > Do you have any idea? > > What that comment is saying is that you can replace an array eg > [a,b,c] with a hash eg {1 => a, 2=> b, 3=>c} > Generating those keys is up to your client side code, approaches > included using a number you increment or going for some unique thing > you generate on the spot (eg time in milliseconds concatenated with a > random number) so that you don''t have to keep track of how many fields > and what not you have created clientside. > > Fred--~--~---------~--~----~------------~-------~--~----~ 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@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Frederick Cheung
2009-Apr-15 16:39 UTC
Re: Complex forms: only one level of “arrayness” is allowed?
On Apr 15, 5:09 pm, PJ <camillieri...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Thanks Fred. > > Considering there is a bit of "wizardry" that needs to be done and > that Rails doesn''t include helpers to do this simply, does it mean it > is not recommended to build such complex forms? > > I am not very experienced with Rails, but I love the fact it is very > "clean" and I feel a bit cautious having to generate keys manually. >There is the nested forms stuff in rails 2.3 (unfortunately not yet documented in the guide). Fred> PJ > > On Apr 15, 9:05 am, Frederick Cheung <frederick.che...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> > wrote: > > > On Apr 15, 1:07 am, PJ <camillieri...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > "model_a[model_b_attributes][][model_c_attributes][][an_attribute]" > > > > But it doesn''t seem to work. I read herehttp://guides.rubyonrails.org/form_helpers.html > > > that "only one level of “arrayness” is allowed" and that I should use > > > hashes instead, but I am not quite sure I understand how to do it. > > > > Do you have any idea? > > > What that comment is saying is that you can replace an array eg > > [a,b,c] with a hash eg {1 => a, 2=> b, 3=>c} > > Generating those keys is up to your client side code, approaches > > included using a number you increment or going for some unique thing > > you generate on the spot (eg time in milliseconds concatenated with a > > random number) so that you don''t have to keep track of how many fields > > and what not you have created clientside. > > > Fred--~--~---------~--~----~------------~-------~--~----~ 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@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Marnen Laibow-Koser
2009-Apr-15 20:30 UTC
Re: Complex forms: only one level of “arrayn ess” is allowed?
PJ wrote: [...]> The natural > indexing should (I think) be: > > "model_a[model_b_attributes][][model_c_attributes][][an_attribute]" > > But it doesn''t seem to work.[...] There is a bug in Rails at least up to 2.2.2 that you may be running into. I reported it on this list around Christmas 2008. If you search the archives for my name around that time, you will find the ensuing discussion and a patch that Fred submitted for the bug. Good luck! Best, -- Marnen Laibow-Koser http://www.marnen.org marnen-sbuyVjPbboAdnm+yROfE0A@public.gmane.org -- 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?hl=en -~----------~----~----~----~------~----~------~--~---