You all probably know the Rails helper function ‘cycle’, most commonly used to specify alternating classes when iterating over a collection :


<% for item in @items %>
  <tr class=<%=cycle('even', 'odd')%>>
    <td><%= item.name %></td>
  </tr>
<% end %>

Well, apparently it is even more usefull than I thought,
and it was simply a matter of reading the rails documentation

  • You can cycle through more than two values: cycle(‘first’, ‘second’, ‘third’, ‘and_more’)
  • Sometimes you need multiple (nested) cycles in your views, for example, the normal alternating rows within a series of alternating tables.
    In that case you can pass the :name key to create ‘named cycles’
    Check the rails documentation for an example.
  • And last bun not least: reset_cycle(‘cycle_name’)
    This makes sure that on each iteration, you will start again with your first value of the cycle list !

Maybe nothing special, but if you didn’t knew this yet, I’m sure you’ll like it :-)

Tail it !

blog comments powered by Disqus

Entries per category

  1. 9 pages are tagged with documentum
  2. 12 pages are tagged with events
  3. 14 pages are tagged with rails
  4. 32 pages are tagged with ruby
  5. 13 pages are tagged with sharepoint

Recent Comments

Popular Threads