Thursday, January 28, 2010

Alternate row colors for table rows in Ruby on Rails

Use the rails builtin helper "cycle" to add alternate row colors to the table rows in a loop.

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

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

<%- end -%>


Then in your stylesheet add below

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

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

0 comments: