Partial loop counter override by local variable

MarkMT
Sun, 03 Aug 2008 22:35:52 -0000

The following is an issue I raised over in the Rails Talk group and it
was suggested that I bring it up here...

Although not documented as far as I''m aware, prior to Rails 2.1 it was
possible to pass a local variable to a partial with the same name as
the partial''s internal collection counter variable, i.e.
<name>_counter, and have it take on the value that is passed. I have
found this to be extremely useful in situations where I have a
collection of items to be rendered, but also want to be able to add an
item to the page one at a time after it has initially been rendered,
in response to an ajax request, e.g. so I can allow the user to add
new fields to a form.

In a situation like this the ordinality of the new item relative to
the others may be important to how it is rendered, but of course the
internal counter disappears once the initial collection has been
rendered. What I had been doing here was to have an rjs template
invoke the same partial used for the collection but on an object, and
pass the counter in as a local to ensure that the object gets treated
the right way with respect to the items already rendered.

Unfortunately, the ability to do this appears to have disappeared in
2.1, and leaves me resorting to some pretty nasty stuff to achieve the
same effect. It appears that regardless of the value passed to the
local variable, when the partial is invoked on an object, internally
the variable is just set to zero.

This seems like a step backwards. Anyone want to explain the thinking
behind this, or suggest an elegant alternative strategy for
implementing the kind of case I described?