Skip to content

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;
}

View the snippets archive ››

1 comment made

Marra commented on 19 Jun 08 at 14:10

This rocks, thank you!

Got something to say?

About

Paul is a web developer for Kyanmedia web agency. He's lucky enough to write in Ruby on Rails full-time and uses this site to post snippets of code.

Contact

my name at gmail.com

More snippets

Take a look in the archive

Need a website?

Contact my employer. Make sure to check out our portfolio of work.

Hosting

I recommend hostingrails.com