RailsCasts Pro episodes are now free!
Learn more or hide this
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.
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 :).
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?
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.
@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.
I enjoy watching your shows. Thanks from Germany!
your screencasts are short, extremely useful, understandable and kinda cool even.
Thanks for your good work! Your screencasts are a real time saver.
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).
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:
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.
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 %>
<% unless task.nil? %>
<% end %>
<% end %>
Thanks for bringing that up, good question.
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.
Probably the easier way to do that with a table is to use something like this:
<% @tasks.in_groups_of(4, false) do |row_tasks| %>
<% for task in row_tasks %>
<p><%= task.name %></p>
<% end %>
<% end %>
(I don't know if it's possible to have each element in a cell, probably yes, but will be more complicated)
Well, I decided that I needed an actual in_vertical_groups_of. I made a post over at ruby-forum about it.
I think that Array::transpose is what you want. That should just invert the matrix and you should be good to go.
Using transpose for top-down layout:
<% n = 3 %>
<% a.in_groups_of((a.length/n).ceil).transpose.each do |row| %>
Came back here five years later to remind myself how to do this. Thanks Ryan. Thank you very much
This episode has been updated for Rails 5 as a blog post. In Groups Of in Rails 5
First sign in through GitHub to post a comment.