Based on this from AWDWR 2nd ed.: "If you need to edit multiple objects from the same model on one form, add open and closed brackets to the name of the instance variable you pass to the form helpers. This tells Rails to include the object’s id as part of the field name. For example, the following template lets a user alter one or more image URLs associated with a list of products." Wouldn''t it be nice if you could do this kind of thing to _non-saved_ model objects (which don''t have ids yet) as well? Maybe the open/closed brackets could indicate to include the index of the object in the collection if the object were a new_record? and then you could more easily update attributes on the objects in that collection. As it stands, I have to set up and maintain a "name unique-ing" scheme for my form fields and know about that scheme in a controller when I go to save the object. Make sense? Thanks, Wes -- 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 -~----------~----~----~----~------~----~------~--~---
Mark Reginald James
2006-Nov-02 23:49 UTC
Re: Wish list: form helpers for collections of new_record?s
Wes Gamble wrote:> Based on this from AWDWR 2nd ed.: > > "If you need to edit multiple objects from the same model on one form, > add > open and closed brackets to the name of the instance variable you pass > to > the form helpers. This tells Rails to include the object’s id as part of > the field > name. For example, the following template lets a user alter one or more > image > URLs associated with a list of products." > > Wouldn''t it be nice if you could do this kind of thing to _non-saved_ > model objects (which don''t have ids yet) as well? Maybe the open/closed > brackets could indicate to include the index of the object in the > collection if the object were a new_record? and then you could more > easily update attributes on the objects in that collection.You can do this easily with the :index option. e.g. <% @collection.each_with_index do |@item, i| %> <%= text_field :item, :method1, :index => i %> <%= text_field :item, :method2, :index => i %> <% end %> posts params {:item => {1 => {:method1 => ''a'', :method2 => ''b''}, 2 => {:method1 => ''c'', :method2 => ''d''}}} You can also use square brackets to get automatic indexing of form fields into param arrays: <% @collection.each do |item| %> <%= text_field_tag ''item[method1][]'', item.method1 %> <%= text_field_tag ''item[method2][]'', item.method2 %> <% end %> posts params {:item => {:method1 => [''a'', ''c''], :method2 => [''b'', ''d'']}} -- 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 -~----------~----~----~----~------~----~------~--~---
Wes Gamble
2006-Nov-07 16:02 UTC
Re: Wish list: form helpers for collections of new_record?s
Mark Reginald James wrote:> You can do this easily with the :index option. e.g. > > <% @collection.each_with_index do |@item, i| %> > <%= text_field :item, :method1, :index => i %> > <%= text_field :item, :method2, :index => i %> > <% end %> > > posts params > > {:item => {1 => {:method1 => ''a'', :method2 => ''b''}, > 2 => {:method1 => ''c'', :method2 => ''d''}}} > > > You can also use square brackets to get automatic > indexing of form fields into param arrays: > > <% @collection.each do |item| %> > <%= text_field_tag ''item[method1][]'', item.method1 %> > <%= text_field_tag ''item[method2][]'', item.method2 %> > <% end %> > > posts params > > {:item => {:method1 => [''a'', ''c''], :method2 => [''b'', ''d'']}}Yep, this is basically the approach that I ended up with - what''s nice is that it works regardless of whether or not the collection members are actually saved. -- 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 -~----------~----~----~----~------~----~------~--~---