Hi, I''d like iterate through a model object(its an array object as you know) in the view so that it displays 2 database table rows in one html table row(in separate columns <td>) and then moves to display the next 2 database table rows in the next html table row. Like this: <table> <tr> <td>array[0]</td> <td>array[1]</td> <tr> <tr> <td>array[2]</td> <td>array[3]</td> <tr> ...... </table> Im kinda clueless about this. Any ideas? Thanks Petr -- 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 -~----------~----~----~----~------~----~------~--~---
On 10/17/06, Petr <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > Hi, > I''d like iterate through a model object(its an array object as you know) > in the view so that it displays 2 database table rows in one html table > row(in separate columns <td>) and then moves to display the next 2 > database table rows in the next html table row. > > Like this: > > <table> > <tr> > <td>array[0]</td> > <td>array[1]</td> > <tr> > > <tr> > <td>array[2]</td> > <td>array[3]</td> > <tr> > > ...... > > </table> > > Im kinda clueless about this. Any ideas? > Thanks > > Petr >Either the rails only way: some_array.in_groups_of(2) which pads with nil, or the more ruby way: require ''enumerator'' some_array.slice(2) Cheers, Max --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Thanks. But, you forgot to mention how to bring that into html, so if you could do that it would be great. Thanks again, Petr Max Muermann wrote:> On 10/17/06, Petr <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote: >> <tr> >> >> </table> >> >> Im kinda clueless about this. Any ideas? >> Thanks >> >> Petr >> > > Either the rails only way: > > some_array.in_groups_of(2) > > which pads with nil, or the more ruby way: > > require ''enumerator'' > > some_array.slice(2) > > Cheers, > Max-- 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 -~----------~----~----~----~------~----~------~--~---
I asked this yesterday in a post titled "Going through an array in batches..." Hope this helps: #in_groups_of http://weblog.rubyonrails.org/2006/3/1/new-in-rails-enumerable-group_by-and-array-in_groups_of <% @products.in_groups_of(4) do |row| %> <tr> <% row.each do |product| %> <td><%= product.name %></td> <% end %> </tr> <% end -%> -- 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 -~----------~----~----~----~------~----~------~--~---
On 10/18/06, Petr <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > Thanks. > > But, you forgot to mention how to bring that into html, so if you could > do that it would be great. > > Thanks again, > Petr >Use in place of some_list.each. Instead of <% for thing in list %> ... render stuff ... <% end %> use <% list.in_groups_of(2) do |two_things| %> <tr> <td><%= two_things[0].some_attribute if two_things[0] %></td> <td><%= two_things[1].some_attribute if two_things[1] %></td> </tr> <% end %> The nil check is necessary, as in_groups_of pads the last "row" with nils if there are not enough elements in the list. BTW, googling for "rails in_groups_of" would have gotten you there as well... Cheers, Max --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---