Alternate row classes with Ruby on Rails

Posted on 23 March 2006

Using the built in TextHelper cycle method makes alternate row classes so easy in Ruby on Rails.

If your table rows are being ouput via a loop then you can use cycle() to automatically use alternate classes for each row...


<%- for item in @items do -%>

  <tr class="<%= cycle("even", "odd") %>">
    ... use item ...
  </tr>

<%- end -%> 

Then in my CSS i'd use something like:


tr.even td{
	background-color:#CCC;
}

tr.odd td{
	background-color:#EEE;
}

Comments left...

  • This rocks, thank you!

    Marra at 19 Jun 08 at 14:10

  • Awesome. Thanks so much!

    Ann at 13 Jan 09 at 12:49

  • it a very good site for learning especially description ….well done

    Tahir at 23 Apr 09 at 22:53

Got something to say?