# Partial loop counter in Rails 2.1

**URL:** <https://www.ruby-forum.com/t/partial-loop-counter-in-rails-2-1/144789>\
**Category:** Rails\
**Created:** [August 2, 2008, 1:17am UTC](https://www.ruby-forum.com/t/partial-loop-counter-in-rails-2-1/144789 "2008-08-02T01:17:14Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![markmt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/df705f/32.png) [@markmt](https://www.ruby-forum.com/u/markmt)\
**Post date:** [August 2, 2008, 1:17am UTC](https://www.ruby-forum.com/t/partial-loop-counter-in-rails-2-1/144789/1 "2008-08-02T01:17:14Z")

</div>

I’m aware that the “\_counter” variable that’s accessible when  
rendering partials with collections was modified in Rails 2.1 so that  
it starts from 1 instead of 0. However I’m wondering if I might have  
discovered another change related to this that I haven’t read about  
anywhere.

In Rails 2.0, I could pass a variable called \_counter to  
a partial as a local variable and it would take on the value that was  
passed rather than one generated automatically. I’ve found this useful  
when invoking the partial from an rjs template in response to an ajax  
request, e.g. to add an extra row to a table after it has initially  
been displayed.

This worked fine in Rails 2.0. For some reason in the application I’m  
currently developing in 2.1 it doesn’t seem to work. Is this a known  
change from 2.0, or am I maybe just doing something else wrong that’s  
preventing it from working (which is completely possible)?

---

<div class="post-metadata">

**Author:** ![markmt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/df705f/32.png) [@markmt](https://www.ruby-forum.com/u/markmt)\
**Post date:** [August 2, 2008, 1:21am UTC](https://www.ruby-forum.com/t/partial-loop-counter-in-rails-2-1/144789/2 "2008-08-02T01:21:55Z")

</div>

On 2 Aug 2008, at 00:16, MarkMT wrote:

> I’m aware that the “\_counter” variable that’s accessible when  
> rendering partials with collections was modified in Rails 2.1 so that  
> it starts from 1 instead of 0. However I’m wondering if I might have  
> discovered another change related to this that I haven’t read about  
> anywhere.

It was changed back on edge

> In Rails 2.0, I could pass a variable called \_counter to  
> a partial as a local variable and it would take on the value that was  
> passed rather than one generated automatically. I’ve found this useful  
> when invoking the partial from an rjs template in response to an ajax  
> request, e.g. to add an extra row to a table after it has initially  
> been displayed.

This sounds like an implementation detail (eg what gets set first? the  
automatic value or the manually specificied one) There was quite a lot  
of refactoring so i wouldn’t be surprised if something ill-defined  
like this changed.

Fred

---

<div class="post-metadata">

**Author:** ![markmt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/df705f/32.png) [@markmt](https://www.ruby-forum.com/u/markmt)\
**Post date:** [August 2, 2008, 5:44am UTC](https://www.ruby-forum.com/t/partial-loop-counter-in-rails-2-1/144789/3 "2008-08-02T05:44:59Z")

</div>

Thanks. This is kind of aggravating. The original behavior seemed  
pretty intuitive and enabled me to do exactly what I wanted to very  
easily. If I’m right in my understanding of what’s happening now, it’s  
forcing me into a pretty nasty workaround. It appears that it the  
partial is called with an object instead of a collection, the internal  
counter is just set to zero. I’d be interested to know if anyone else  
can confirm that this is what happens.

Mark.

On Aug 1, 6:21 pm, Frederick C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![markmt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/df705f/32.png) [@markmt](https://www.ruby-forum.com/u/markmt)\
**Post date:** [August 2, 2008, 11:52am UTC](https://www.ruby-forum.com/t/partial-loop-counter-in-rails-2-1/144789/4 "2008-08-02T11:52:39Z")

</div>

On 2 Aug 2008, at 05:59, MarkMT wrote:

> Just to clarify and confirm what I said before - if a partial is  
> called with an object instead of a collection, the internal counter is  
> set to zero - even if the invoking template passes in a local variable  
> with the same name as the counter, ie \_counter. Unlike  
> other local variables, this one has no effect.So you can no longer  
> override the counter by passing a local variable.

Just had a look at the code and that makes sense. Might be worth  
asking on rails-core about this, as I can certainly see that this is a  
useful think to be able to override, especially as you point out in  
the case where you’re rendering a particular row again for javascript.

Fred

---

<div class="post-metadata">

**Author:** ![markmt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/df705f/32.png) [@markmt](https://www.ruby-forum.com/u/markmt)\
**Post date:** [August 4, 2008, 12:14am UTC](https://www.ruby-forum.com/t/partial-loop-counter-in-rails-2-1/144789/5 "2008-08-04T00:14:54Z")

</div>

On 2 Aug 2008, at 10:52, Frederick C. wrote:

> > other local variables, this one has no effect.So you can no longer  
> > override the counter by passing a local variable.
> 
> Just had a look at the code and that makes sense. Might be worth  
> asking on rails-core about this, as I can certainly see that this is  
> a useful think to be able to override, especially as you point out  
> in the case where you’re rendering a particular row again for  
> javascript.

I had a look and in edge rails you can once more override the counter  
variable.

Fred

---

<div class="post-metadata">

**Author:** ![markmt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/df705f/32.png) [@markmt](https://www.ruby-forum.com/u/markmt)\
**Post date:** [August 4, 2008, 4:47am UTC](https://www.ruby-forum.com/t/partial-loop-counter-in-rails-2-1/144789/6 "2008-08-04T04:47:55Z")

</div>

Cool - thanks!

On Aug 3, 5:14 pm, Frederick C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![markmt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/df705f/32.png) [@markmt](https://www.ruby-forum.com/u/markmt)\
**Post date:** [August 2, 2008, 7:00am UTC](https://www.ruby-forum.com/t/partial-loop-counter-in-rails-2-1/144789/7 "2008-08-02T07:00:24Z")

</div>

Just to clarify and confirm what I said before - if a partial is  
called with an object instead of a collection, the internal counter is  
set to zero - even if the invoking template passes in a local variable  
with the same name as the counter, ie \_counter. Unlike  
other local variables, this one has no effect.So you can no longer  
override the counter by passing a local variable.
