For displaying lists it is often useful to show the rows of the list in alternating colors. Ruby on Rails makes this really easy.
- Define the background colors using HTML style tag.
- Add the class attribute that calls the
cyclemethod to the HTML element that you use to describe the row, e.g.,DIVor table rowTR, …, for example:
<% for track in @tracks %>
<div class="<%= cycle('even','odd') %>" >
<h3> 'show', :id => track %></h3>
</div>
<% end %>
<head>
<style>
.even { background: #99ccff; }
.odd { background: #cccccc; }
</style>
</head>
That is it. Kudos to Ruby on Rails creators for making this so simple!
May 31, 2007 at 3:39 pm |
Where does the “cycle” come from? Is that a Rails-builtin?
June 1, 2007 at 3:27 am |
Rails defines a framework in terms of APIs. It includes a template engine (similar to Java JSP), e.g. The APIs includes a number of very useful utility classes. One of them is called ActionView::Helpers::TextHelper and it includes a method called cycle.
May 11, 2008 at 10:15 am |
perfect! i was using my own helper, no sense reinventing the wheel. -thanks!