Hidden counter in partial collections

February 4th, 2008 by Jo Potts Leave a reply »

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.

Advertisement

1 comment

  1. Jo Potts says:

    Update:

    View the official api doc on this here:
    http://api.rubyonrails.org/classes/ActionView/Partials.html

    2nd paragraph of ‘Rendering a collection of partials’

Leave a Reply