#28
May 07, 2007

in_groups_of

Have you ever wanted to visually line up items in rows and columns? The in_groups_of method makes this a cinch. Just watch out for the gotcha.
Download (15.3 MB, 4:25)
alternative download for iPod & Apple TV (7 MB, 4:25)
<!-- tasks/index.rhtml -->
<table>
<% @tasks.in_groups_of(4, false) do |row_tasks| %>
  <tr>
    <% for task in row_tasks %>
      <td><%= task.name %></td>
    <% end %>
  </tr>
<% end %>
</table>

RSS Feed for Episode Comments 24 comments

1. eTrueke May 07, 2007 at 04:56

Hi!

First of all, congratulations.

And second, I've a suggestion; Why not to use a ul,li list? We can write CSS to display it as a columns and the MVC will be respected.


2. Jengibre May 07, 2007 at 05:58

Extremely useful!


3. InMan May 07, 2007 at 07:35

Hey Ryan.
Just for info: Your add comment labels have mistype.
<label for"comment_name" class="required">Name:</label>
Should be for="comment_name"

Thanks for railscast :).


4. Rebort May 07, 2007 at 07:57

I am addicted to your site. Great tips once again!

I remember creating this sort of fucntionality in PHP about eight years ago -- pre CSS days and all -- and it was a bit of a nightmare.

What's the advantage, though, of using Rails for this instead of formatting with CSS?


5. Slaptijack May 07, 2007 at 08:01

Although it might be easier to accomplish this particular task via CSS, I think it's also a simple way to show the functionality of the in_groups_of() method.


6. Ryan Bates May 07, 2007 at 08:10

@InMan, thanks. I'll fix that.

@eTrueke & Rebort, if CSS will work for you, then that is definitely preferred. But sometimes you need the exact behavior only a table can provide, where you need everything to line up and you don't want the columns to be fixed width. I'm no CSS guru so I may just be missing something.

@Slaptijack, right, this was more of a way to show off in_groups_of then to push tables. You might, for example, need to use it while generating a PDF document where the rows and columns need to be strictly defined.


7. Tobias May 07, 2007 at 08:31

I enjoy watching your shows. Thanks from Germany!


8. jeroen May 07, 2007 at 10:23

your screencasts are short, extremely useful, understandable and kinda cool even.


9. Croaker May 07, 2007 at 13:43

Thanks for your good work! Your screencasts are a real time saver.


10. chineseGuy May 08, 2007 at 22:07

perfect!
完美!


11. Wincent Colaiuta May 09, 2007 at 07:48

In the last example where you use in_groups_of(4, false) does that yield valid X/HTML? ie. the last table row will not have as many TD elements as the previous rows. Don't know if that's allowed or not (it may be).


12. Benjie May 14, 2007 at 12:08

i've got the same question Wincent. I'm wondering if there isn't a way to conditionally act upon the nil objects.
I'm really interested in learning more about this technique.
Here's what I want to do:

http://www.scubadorag.com/coco/links/USAlinks.asp

We will be moving to a rails host. I've thought about doing it with modulo, but I haven't been able to wrap my head around it yet.


13. Ryan Bates May 14, 2007 at 15:01

Hmm, I'm not sure if it's valid XHTML or not. If not, you could do a nil check inside the td element like this:

<% for task in tasks_row %>
  <td>
    <% unless task.nil? %>
      ...
    <% end %>
  </td>
<% end %>

Thanks for bringing that up, good question.


14. Phillip Dec 04, 2007 at 18:08

I'm a little late getting in this game, but I've got a question I haven't been able to find the answer to. Your example displays the groups horizontally. Is there a way to display them vertically? I'd like to use this method in displaying a directory and I'd like to go down the columns first, then left to right. And since I'm not a CSS guru either, I thought I'd do it with tables.

Thanks.


15. cover Dec 24, 2007 at 15:55

@Phillip:
Probably the easier way to do that with a table is to use something like this:

<table><tr>
<% @tasks.in_groups_of(4, false) do |row_tasks| %>
  <td>
    <% for task in row_tasks %>
      <p><%= task.name %></p>
    <% end %>
  </td>
<% end %>
</tr></table>

(I don't know if it's possible to have each element in a cell, probably yes, but will be more complicated)


16. Phillip Jan 01, 2008 at 11:13

Well, I decided that I needed an actual in_vertical_groups_of. I made a post over at ruby-forum about it.

http://www.ruby-forum.com/topic/137230

Peace,
Phillip


17. Lou Jan 14, 2008 at 12:03

@Phillip

I think that Array::transpose is what you want. That should just invert the matrix and you should be good to go.

--
Lou


18. ben Sep 03, 2008 at 11:24

Using transpose for top-down layout:

<% n = 3 %>
<% a.in_groups_of((a.length/n).ceil).transpose.each do |row| %>

taken from

http://ruby-on-the-interrails.blogspot.com/2008/09/method-of-day-arrayingroupsof.html


19. dafs Aug 05, 2009 at 23:18

Following the initial [URL=http://www.realpoor.co.uk]wow gold[/URL] White House [URL=http://www.realpoor.co.uk]buy wow gold[/URL] Forum on [URL=http://www.realpoor.co.uk]wow leveling[/URL] Health Reform [URL=http://www.realpoor.co.uk]wow gold cheap[/URL], and then [URL=http://www.wowgoldbag.co.uk]wow gold[/URL] the regional [URL=http://www.wowgoldbag.co.uk]buy wow gold[/URL] forums across [URL=http://www.wowgoldbag.co.uk]wow power leveling[/URL] the country, [URL=http://www.wowgoldbag.co.uk]cheapest wow gold[/URL] we began [URL=http://www.mmotao.co.uk]wow gold[/URL] holding what [URL=http://www.mmotao.co.uk]world of warcraft gold[/URL] we called[URL=http://www.mmotao.co.uk]buy wow gold[/URL] Health Care[URL=http://www.mmotao.co.uk]cheap wow gold[/URL] Stakeholder [URL=http://www.mmotao.co.uk]wow leveling[/URL] Discussions [URL=http://www.srogold.co.uk]wow gold[/URL] here at the [URL=http://www.srogold.co.uk]world of warcraft gold[/URL] White House [URL=http://www.srogold.co.uk]buy wow gold[/URL]. Keeping with [URL=http://www.srogold.co.uk]cheap wow gold[/URL] the President’s [URL=http://www.srogold.co.uk]wow power leveling[/URL] dedication to [URL=http://www.srogold.co.uk]wow powerleveling[/URL] transparency in [URL=http://www.srogold.co.uk/the-lord-of-the-ring-eu-gold-1140.html]Lord of the Rings Online Gold[/URL] the health [URL=http://www.srogold.co.uk/the-lord-of-the-ring-eu-gold-1140.html]LOTRO Gold[/URL] reform process [URL=http://www.srogold.co.uk/the-lord-of-the-ring-eu-gold-1140.html]LOTR Gold[/URL], we streamed [URL=http://www.srogold.co.uk/fly-for-fun-gold-1517.html]fly for fun penya[/URL] these meetings [URL=http://www.srogold.co.uk/fly-for-fun-gold-1517.html]flyff penya[/URL] live here [URL=http://www.srogold.co.uk/fly-for-fun-gold-1517.html]buy flyff gold[/URL] at WhiteHouse [URL=http://www.srogold.co.uk/final-fantasy-xi-gold-11.html]Final Fantasy XI gil[/URL].gov.It took [URL=http://www.srogold.co.uk/final-fantasy-xi-gold-11.html]ffxi gil[/URL] quite a bit of [URL=http://www.srogold.co.uk/warhammer-online-eu-gold-2554.html]buy Warhammer gold[/URL] processing, [URL=http://www.srogold.co.uk/warhammer-online-eu-gold-2554.html]Warhammer gold[/URL] but we’[URL=http://www.srogold.co.uk/ever-quest-2-gold-1257.html]EverQuest 2 gold[/URL] re pleased to [URL=http://www.srogold.co.uk/ever-quest-2-gold-1257.html]eq2 plat[/URL] post the [URL=http://www.srogold.co.uk/dofus-gold-1482.html]dofus kamas[/URL] video of eight [URL=http://www.srogold.co.uk/dofus-gold-1482.html]kamas dofus[/URL] of these meetings [URL=http://www.srogold.co.uk/runescape-gold-1518.html]runescape money[/URL] below on a [URL=http://www.srogold.co.uk/runescape-gold-1518.html]runescape gold[/URL] wide variety [URL=http://www.tbcgold.co.uk]wow gold[/URL] of subjects. All [URL=http://www.tbcgold.co.uk]world of warcraft gold[/URL] of them were [URL=http://www.tbcgold.co.uk]buy wow gold[/URL] very interesting [URL=http://www.tbcgold.co.uk]cheap wow gold[/URL] conversations [URL=http://www.tbcgold.co.uk]wow power leveling[/URL], and once [URL=http://www.tbcgold.co.uk]wow powerleveling[/URL] we got the technology [URL=http://www.tbcgold.co.uk/the-lord-of-the-ring-eu-gold-1140.html]Lord of the Rings Online Gold[/URL] up and [URL=http://www.tbcgold.co.uk/the-lord-of-the-ring-eu-gold-1140.html]LOTRO Gold[/URL] running we even [URL=http://www.tbcgold.co.uk/the-lord-of-the-ring-eu-gold-1140.html]LOTR Gold[/URL] found ways to include [URL=http://www.tbcgold.co.uk/fly-for-fun-gold-1516.html]fly for fun penya[/URL] feedback [URL=http://www.tbcgold.co.uk/fly-for-fun-gold-1516.html]flyff penya[/URL] from our online [URL=http://www.tbcgold.co.uk/fly-for-fun-gold-1516.html]buy flyff gold[/URL] chats in [URL=http://www.tbcgold.co.uk/final-fantasy-xi-gold-11.html]Final Fantasy XI gil[/URL] the meetings [URL=http://www.tbcgold.co.uk/final-fantasy-xi-gold-11.html]>ffxi gil[/URL]. You can read more [URL=http://www.tbcgold.co.uk/warhammer-online-eu-gold-2554.html]buy Warhammer gold[/URL] about the President’s [URL=http://www.tbcgold.co.uk/warhammer-online-eu-gold-2554.html]Warhammer gold[/URL] eight health [URL=http://www.tbcgold.co.uk/ever-quest-2-gold-1257.html]EverQuest 2 gold[/URL] insurance consumer [URL=http://www.tbcgold.co.uk/ever-quest-2-gold-1257.html]eq2 plat[/URL] protections here [URL=http://www.tbcgold.co.uk/dofus-gold-1481.html]dofus kamas[/URL], and figure [URL=http://www.tbcgold.co.uk/dofus-gold-1481.html]kamas dofus[/URL] out how reform will [URL=http://www.tbcgold.co.uk/runescape-gold-1517.html]Runescape Money[/URL] directly affect you and [URL=http://www.tbcgold.co.uk/runescape-gold-1517.html]Runescape gold[/URL] your family.


20. Victor Yoalli Nov 21, 2009 at 13:18

Great railcast as always.
Just reported spam as promise.

Thanks


21. cheap sto credit Apr 14, 2010 at 02:11

great


22. cheap sto credits Apr 15, 2010 at 02:12

i like your post


23. Queer As Folk DVD set Jul 14, 2010 at 18:37

hi guys,why not try some medium, high quality life needs it, i suppose you are a crazy movie lover like me:p, there are one wonderful DVD movie i really love and want to share with you. Queer As Folk DVD really has amazing plot , wonderful screen, and also nice musics. Don't miss it!!! if you want Queer As Folk DVD, Queer As Folk DVD set , Queer As Folk DVD boxset or Queer As Folk DVD Seasons 1-5 DVD boxset, just go to http://www.buydvdezy.com/goods-322-Queer-As-Folk-Seasons-1-5-DVD-boxset.html i found. it is very helpful~~~
http://www.buydvdezy.com/goods-322-Queer-As-Folk-Seasons-1-5-DVD-boxset.html
pass it? Never!


24. Queer As Folk DVD boxset Jul 14, 2010 at 18:38

hi guys,why not try some medium, high quality life needs it, i suppose you are a crazy movie lover like me:p, there are one wonderful DVD movie i really love and want to share with you. Queer As Folk DVD really has amazing plot , wonderful screen, and also nice musics. Don't miss it!!! if you want Queer As Folk DVD, Queer As Folk DVD set , Queer As Folk DVD boxset or Queer As Folk DVD Seasons 1-5 DVD boxset, just go to http://www.buydvdezy.com/goods-322-Queer-As-Folk-Seasons-1-5-DVD-boxset.html i found. it is very helpful~~~
http://www.buydvdezy.com/goods-322-Queer-As-Folk-Seasons-1-5-DVD-boxset.html
pass it? Never!


25. Queer As Folk DVD Jul 14, 2010 at 18:38

hi guys,why not try some medium, high quality life needs it, i suppose you are a crazy movie lover like me:p, there are one wonderful DVD movie i really love and want to share with you. Queer As Folk DVD really has amazing plot , wonderful screen, and also nice musics. Don't miss it!!! if you want Queer As Folk DVD, Queer As Folk DVD set , Queer As Folk DVD boxset or Queer As Folk DVD Seasons 1-5 DVD boxset, just go to http://www.buydvdezy.com/goods-322-Queer-As-Folk-Seasons-1-5-DVD-boxset.html i found. it is very helpful~~~
http://www.buydvdezy.com/goods-322-Queer-As-Folk-Seasons-1-5-DVD-boxset.html
pass it? Never!


26. Cheap Gucci sale Jul 27, 2010 at 00:55

Gucci low top shoes is waiting for your shopping cart.Log in to choose the one that fit you best.Several colors and sizes for you to choose.


27. Jordan Sneakers Aug 01, 2010 at 23:23

think that Array::transpose is what you want. That should just invert the matrix and you should be good to go.


28. steelers shop Aug 04, 2010 at 23:34

thanks for sharing.

welcome to our shop

http://www.nflsteelersshop.com/


29. dhnzdfgb Aug 05, 2010 at 08:51

Thank you for the information

 The article is very professionally written. I enjoy reading every day

good url!


30. free directory list Aug 11, 2010 at 22:07

Ryan, could you show the view that is calling this template?


31. free directory list Aug 11, 2010 at 22:07

Ryan, could you show the view that is calling this template?


32. free directory list Aug 11, 2010 at 22:07

Ryan, could you show the view that is calling this template?


33. p90 workout Aug 12, 2010 at 09:15

I want to say that I dont think Ive read anything so true in a long time. Youve got a lot of great ideas, a great deal of perspective. I think that you definitely have something important to say and Im gonna back it 110%, man. Good job keeping this subject alive and interesting!


34. Cheap Nfl Jerseys Aug 13, 2010 at 18:50

Great railcast as always.
Just reported spam as promise.


35. Air Jordan Force 4 Aug 14, 2010 at 02:14

Great railcast as always. Just reported spam as promise. Youve got a lot of great ideas, a great deal of perspective. I think that you definitely have something important to say and Im gonna back it 110%, man.


36. accessorize Aug 20, 2010 at 02:09

The content of the post is very well, from here I know much about sports knowledge, especially about the Puma training shoes. Here, I will introduce the puma lovers some good websites about puma with high price point.


37. accessorize Aug 20, 2010 at 02:12

The content of the post is very well, from here I know much about sports knowledge, especially about the Puma training shoes. Here, I will introduce the puma lovers some good websites about puma with high price point.


38. louis vuitton shoes Aug 26, 2010 at 20:55

Thanks for sharing your article. I really enjoyed it. I put a link to my site to here so other people can read it. My readers have about the same interets


39. snow boots Aug 31, 2010 at 01:53

I remember creating this sort of fucntionality in PHP about eight years ago -- pre CSS days and all -- and it was a bit of a nightmare.


40. louis vuitton sunglasses Sep 01, 2010 at 21:30

I agree with your Blog and I will be back to check it more in the future so please keep up your work. I love your content & the way that you write. It looks like you’ve been doing this for a while now, how long have you been blogging for?

Add your comment:

(SKIP THIS ONE)

(required)

(not shown)


(use pastie or gist for code)

sponsored by:
if you want to help:
required:
Get Quicktime Player
Give Back to Open Source