Rails render partial counters: displaying a record's index in a collection

Posted Posted by Wes in Ruby on Rails     Comments No comments
Apr
9

I always forget that you can reference the index of an item in a collection when you’re using a partial:

<%= render :partial => 'item', :object => @items %>
# in _item.html.erb
<%= item_counter %>

This is zero-based, so you might want to +1 if you want the count to start at 1.

It’s an easy way to number lines in a partial, which I find I do fairly often and I can never remember the syntax for getting that index counter!

Post comment

about databasically

We live and work in Kansas City, USA.

We're passionate about helping small businesses succeed and want to help you use technology to get more done.

From server, desktop, network management to programming custom web applications in Ruby on Rails, we're here to lend a hand.

Contact us if you have any questions!