# Getting an index of an element rendered in partial

**URL:** <https://www.ruby-forum.com/t/getting-an-index-of-an-element-rendered-in-partial/92929>\
**Category:** Rails\
**Created:** [April 25, 2007, 7:59pm UTC](https://www.ruby-forum.com/t/getting-an-index-of-an-element-rendered-in-partial/92929 "2007-04-25T19:59:09Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![g0nzo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/3bc359/32.png) [@g0nzo](https://www.ruby-forum.com/u/g0nzo)\
**Post date:** [April 25, 2007, 7:59pm UTC](https://www.ruby-forum.com/t/getting-an-index-of-an-element-rendered-in-partial/92929/1 "2007-04-25T19:59:09Z")

</div>

Hi!

I’ve got a sorted array of elements. I render the whole array in one  
place, but also the three first elements in another using the following  
command:

“render :partial =\> ‘partial’, :collection =\> @first\_elements”

The problem is that I also need an index of every element in the array  
inside the partial (to correctly create a link to it). Is there a way to  
somehow determine an index of an element inside the partial or maybe to  
somehow pass it along as separate variable?

Thanks in advance

---

<div class="post-metadata">

**Author:** ![g0nzo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/3bc359/32.png) [@g0nzo](https://www.ruby-forum.com/u/g0nzo)\
**Post date:** [April 25, 2007, 10:20pm UTC](https://www.ruby-forum.com/t/getting-an-index-of-an-element-rendered-in-partial/92929/2 "2007-04-25T22:20:26Z")

</div>

On Apr 25, 2007, at 1:59 PM, Szymon N. wrote:

> inside the partial (to correctly create a link to it). Is there a  
> way to  
> somehow determine an index of an element inside the partial or  
> maybe to  
> somehow pass it along as separate variable?
> 
> Thanks in advance

The index of the element in the collection for :partial =\> ‘foo’ is a  
local variable foo\_counter available within the partial.

However, you might consider using the element’s id as the basis for  
linking.

Also, you could render :partial =\> ‘partial’, :collection =\>  
@elements.first(3)

You can also pass arbitrary variables into a partial with :locals =\>  
{ :bar =\> ‘iron’ } which makes a local named bar available to the  
partial with a value of ‘iron’.

If you have the Agile Web D. with Rails, 2nd ed., see Ch.22:  
Action View, “Layouts and Components” pp.509-511 (PDF pp.510-512).

-Rob

Rob B. [http://agileconsultingllc.com](http://agileconsultingllc.com)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![g0nzo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/3bc359/32.png) [@g0nzo](https://www.ruby-forum.com/u/g0nzo)\
**Post date:** [April 26, 2007, 6:07pm UTC](https://www.ruby-forum.com/t/getting-an-index-of-an-element-rendered-in-partial/92929/3 "2007-04-26T18:07:23Z")

</div>

I can’t use IDs, because the table is sorted by values from another  
column and I need to paginate through this sorted table. But the  
foo\_counter is exactly what I need.

Thank you very much!

Rob B. wrote:

> On Apr 25, 2007, at 1:59 PM, Szymon N. wrote:
> 
> > inside the partial (to correctly create a link to it). Is there a  
> > way to  
> > somehow determine an index of an element inside the partial or  
> > maybe to  
> > somehow pass it along as separate variable?
> > 
> > Thanks in advance
> 
> The index of the element in the collection for :partial =\> ‘foo’ is a  
> local variable foo\_counter available within the partial.
> 
> However, you might consider using the element’s id as the basis for  
> linking.
> 
> Also, you could render :partial =\> ‘partial’, :collection =\>  
> @elements.first(3)
> 
> You can also pass arbitrary variables into a partial with :locals =\>  
> { :bar =\> ‘iron’ } which makes a local named bar available to the  
> partial with a value of ‘iron’.
> 
> If you have the Agile Web D. with Rails, 2nd ed., see Ch.22:  
> Action View, “Layouts and Components” pp.509-511 (PDF pp.510-512).
> 
> -Rob
> 
> Rob B. [http://agileconsultingllc.com](http://agileconsultingllc.com)  
> removed\_email\_address@domain.invalid
