Partial loop counter in Rails 2.1

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)?

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

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. [email protected]

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

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

Cool - thanks!

On Aug 3, 5:14 pm, Frederick C. [email protected]

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.