1
0
mirror of https://github.com/jbranchaud/til synced 2026-01-05 08:08:02 +00:00

Add Where Am I In The Partial Iteration as a rails til

This commit is contained in:
jbranchaud
2016-06-09 08:41:18 -05:00
parent 84d5b7f19a
commit e3a43d21a9
2 changed files with 24 additions and 1 deletions

View File

@@ -0,0 +1,22 @@
# Where Am I In The Partial Iteration?
Let's say I am going to render a collection of posts with a post partial.
```erb
<%= render collection: @posts, partial: "post" %>
```
The
[`ActionView::PartialIteration`](http://api.rubyonrails.org/classes/ActionView/PartialIteration.html)
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](http://stackoverflow.com/questions/13397848/rails-render-collection-partial-getting-size-of-collection-inside-partial)
h/t Josh Davey