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 -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Rob Biedenharn
2007-Apr-25 20:04 UTC
Re: Getting an index of an element rendered in partial
On Apr 25, 2007, at 1:59 PM, Szymon Nowak wrote:> 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 advanceThe 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-xa9cJyRlE0mWcWVYNo9pwxS2lgjeYSpx@public.gmane.org
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-xa9cJyRlE0mWcWVYNo9pwxS2lgjeYSpx@public.gmane.org-- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---