1 min read

Interesting Rails Feature - Partial Counters

While working on a Ruby on Rails project, I stumbled across an interesting feature.

= render partial: 'product_line', collection: product_lines_with_brand(@result, brand)

In my view I was rendering the partial above. For styling reasons I required that after ever second time the partial is called, I should be adding a clearing div.

After spending some time to try to find a good solution, I stumbled upon something really neat that Rails provides by default. Every partial rendered upon a collection has a counter associated with it. In the case of the partial above, the name of the counter would be product_line_counter. Basically the name of the partial with _counter being appended to the end of it.

So all I had to do was append the following snippet of code at the very end of the partial

- if product_line_counter.odd?
  .clear