diff --git a/README.md b/README.md index ed8d73f..3b06f3f 100644 --- a/README.md +++ b/README.md @@ -7,7 +7,7 @@ variety of languages and technologies. These are things that don't really warrant a full blog post. These are mostly things I learn by pairing with smart people at [Hashrocket](http://hashrocket.com/). -_430 TILs and counting..._ +_431 TILs and counting..._ --- @@ -287,6 +287,7 @@ _430 TILs and counting..._ - [Show Rails Models With Pry](rails/show-rails-models-with-pry.md) - [Show Rails Routes With Pry](rails/show-rails-routes-with-pry.md) - [Truncate Almost All Tables](rails/truncate-almost-all-tables.md) +- [Where Am I In The Partial Iteration?](rails/where-am-i-in-the-partial-iteration.md) ### Ruby diff --git a/rails/where-am-i-in-the-partial-iteration.md b/rails/where-am-i-in-the-partial-iteration.md new file mode 100644 index 0000000..ac90330 --- /dev/null +++ b/rails/where-am-i-in-the-partial-iteration.md @@ -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