Archive for February, 2008

Hidden counter in partial collections

February 4th, 2008

A quick tech post to share knowledge:

If you need a counter, iterator, index, loop variable number type thing inside a partial rendering a collection then you can simply use an undocumented counter called [partialname]_counter.

render :partial => 'parrot', :collection => @parrots

Then in _parrot.html.erb you can use:

parrot_counter

I like this like I like each_with_index. :)

Rails2 foot note: I think the :collection option can be dropped in my example with the names I’ve used here but I’ve left it in for clarity.