Hi, I''m using a bog standard .find(:all) in my controller which the <% for %> loop in my view then displays the results of. How can I apply a different css style (class or id) to the first result that my view returns? and then subsequently the 2nd and 3rd (basically Gold, Silver, Bronze). All the rest just use a generic style. I hope that make sense? Thanks for any help, 3Quid -- 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 -~----------~----~----~----~------~----~------~--~---
On 16 Apr 2008, at 11:11, Graham Dawe wrote:> > Hi, > > I''m using a bog standard .find(:all) in my controller which the <% for > %> loop in my view then displays the results of. > > How can I apply a different css style (class or id) to the first > result > that my view returns? and then subsequently the 2nd and 3rd (basically > Gold, Silver, Bronze). All the rest just use a generic style.You just need to track which row you''re on. If you use render :partial then you automatically get an index variable (if the partial is foo then you get foo_counter) Fred --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
On Apr 16, 2008, at 6:19 AM, Frederick Cheung wrote:> On 16 Apr 2008, at 11:11, Graham Dawe wrote: >> Hi, >> >> I''m using a bog standard .find(:all) in my controller which the <% >> for >> %> loop in my view then displays the results of. >> >> How can I apply a different css style (class or id) to the first >> result >> that my view returns? and then subsequently the 2nd and 3rd >> (basically >> Gold, Silver, Bronze). All the rest just use a generic style. > > You just need to track which row you''re on. If you use render :partial > then you automatically get an index variable (if the partial is foo > then you get foo_counter) > > FredYou could use a hash with a default value and index it with the position: irb> medal_class = Hash.new(''generic'').update(1=>''Gold'', 2=>''Silver'', 3=>''Bronze'') => {1=>"Gold", 2=>"Silver", 3=>"Bronze"} irb> medal_class[1] => "Gold" irb> medal_class[2] => "Silver" irb> medal_class[3] => "Bronze" irb> medal_class[4] => "generic" irb> medal_class[5] => "generic" irb> medal_class[0] => "generic" If you follow Fred''s advice and use a partial for a row, then the *_counter will be a good choice. If you have a simple loop with .each_with_index {|thing,index| ... }, then you''d have to adjust 0=>''Gold'', etc. to match the zero-based array index. -Rob Rob Biedenharn http://agileconsultingllc.com Rob-xa9cJyRlE0mWcWVYNo9pwxS2lgjeYSpx@public.gmane.org --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Personally, I hate <% for %> and prefer to go with the underlying <% each %>... and for your purpose you might also consider using <% each_with_index %>. Combine that with Rob''s suggestion and you can use the index from the loop to index the medal_class hash. On Apr 16, 9:59 am, Rob Biedenharn <R...-xa9cJyRlE0mWcWVYNo9pwxS2lgjeYSpx@public.gmane.org> wrote:> On Apr 16, 2008, at 6:19 AM, Frederick Cheung wrote: > > > > > On 16 Apr 2008, at 11:11, Graham Dawe wrote: > >> Hi, > > >> I''m using a bog standard .find(:all) in my controller which the <% > >> for > >> %> loop in my view then displays the results of. > > >> How can I apply a different css style (class or id) to the first > >> result > >> that my view returns? and then subsequently the 2nd and 3rd > >> (basically > >> Gold, Silver, Bronze). All the rest just use a generic style. > > > You just need to track which row you''re on. If you use render :partial > > then you automatically get an index variable (if the partial is foo > > then you get foo_counter) > > > Fred > > You could use a hash with a default value and index it with the > position: > > irb> medal_class = Hash.new(''generic'').update(1=>''Gold'', 2=>''Silver'', > 3=>''Bronze'') > => {1=>"Gold", 2=>"Silver", 3=>"Bronze"} > irb> medal_class[1] > => "Gold" > irb> medal_class[2] > => "Silver" > irb> medal_class[3] > => "Bronze" > irb> medal_class[4] > => "generic" > irb> medal_class[5] > => "generic" > irb> medal_class[0] > => "generic" > > If you follow Fred''s advice and use a partial for a row, then the > *_counter will be a good choice. If you have a simple loop > with .each_with_index {|thing,index| ... }, then you''d have to adjust > 0=>''Gold'', etc. to match the zero-based array index. > > -Rob > > Rob Biedenharn http://agileconsultingllc.com > R...-xa9cJyRlE0mWcWVYNo9pwxS2lgjeYSpx@public.gmane.org--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---