Skip to content

Commit e3a43d2

Browse files
committed
Add Where Am I In The Partial Iteration as a rails til
1 parent 84d5b7f commit e3a43d2

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ variety of languages and technologies. These are things that don't really
77
warrant a full blog post. These are mostly things I learn by pairing with
88
smart people at [Hashrocket](http://hashrocket.com/).
99

10-
_430 TILs and counting..._
10+
_431 TILs and counting..._
1111

1212
---
1313

@@ -287,6 +287,7 @@ _430 TILs and counting..._
287287
- [Show Rails Models With Pry](rails/show-rails-models-with-pry.md)
288288
- [Show Rails Routes With Pry](rails/show-rails-routes-with-pry.md)
289289
- [Truncate Almost All Tables](rails/truncate-almost-all-tables.md)
290+
- [Where Am I In The Partial Iteration?](rails/where-am-i-in-the-partial-iteration.md)
290291

291292
### Ruby
292293

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Where Am I In The Partial Iteration?
2+
3+
Let's say I am going to render a collection of posts with a post partial.
4+
5+
```erb
6+
<%= render collection: @posts, partial: "post" %>
7+
```
8+
9+
The
10+
[`ActionView::PartialIteration`](http://api.rubyonrails.org/classes/ActionView/PartialIteration.html)
11+
module provides a couple handy methods when rendering collections.
12+
I'll have access in the partial template to `#{template_name}_iteration`
13+
(e.g. `post_iteration`) which will, in turn, give me access to `#index`,
14+
`#first?`, and `#last?`.
15+
16+
This is great if I need to do something special with the first or last item
17+
in the collection or if I'd like to do some sort of numbering based on the
18+
index of each item.
19+
20+
[source](http://stackoverflow.com/questions/13397848/rails-render-collection-partial-getting-size-of-collection-inside-partial)
21+
22+
h/t Josh Davey

0 commit comments

Comments
 (0)