Forum: Ruby on Rails Getting an index of an element rendered in partial

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
F639e0cdf2fa098b535d6ec766b2aee1?d=identicon&s=25 Szymon Nowak (g0nzo)
on 2007-04-25 19:59
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
Ef3aa7f7e577ea8cd620462724ddf73b?d=identicon&s=25 Rob Biedenharn (Guest)
on 2007-04-25 22:20
(Received via mailing list)
On Apr 25, 2007, at 1:59 PM, Szymon Nowak 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 Development with Rails, 2nd ed., see Ch.22:
Action View, "Layouts and Components" pp.509-511 (PDF pp.510-512).

-Rob

Rob Biedenharn    http://agileconsultingllc.com
Rob@AgileConsultingLLC.com
2b2b07fe8688479d2559704d4bf8a264?d=identicon&s=25 Szymon Nowak (szimek)
on 2007-04-26 18:07
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 Biedenharn wrote:
> On Apr 25, 2007, at 1:59 PM, Szymon Nowak 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 Development with Rails, 2nd ed., see Ch.22:
> Action View, "Layouts and Components" pp.509-511 (PDF pp.510-512).
>
> -Rob
>
> Rob Biedenharn    http://agileconsultingllc.com
> Rob@AgileConsultingLLC.com
This topic is locked and can not be replied to.