Getting an index of an element rendered in partial


#1

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


#2

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
removed_email_address@domain.invalid


#3

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
removed_email_address@domain.invalid