Today I Learned

hashrocket A Hashrocket project

Where Am I In The Partial Iteration?

Let's say I am going to render a collection of posts with a post partial.

<%= render collection: @posts, partial: "post" %>

The ActionView::PartialIteration module provides a couple handy methods when rendering collections. I'll have access in the partial template to #{template_name}_iteration (e.g. post_iteration) which will, in turn, give me access to #index, #first?, and #last?.

This is great if I need to do something special with the first or last item in the collection or if I'd like to do some sort of numbering based on the index of each item.

source

h/t Josh Davey

See More #rails TILs
Looking for help? Hashrocket has been an industry leader in Ruby on Rails since 2008. Rails is a core skill for each developer at Hashrocket, and we'd love to take a look at your project. Contact us and find out how we can help you.