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 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
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:
medal_class = Hash.new('generic').update(1=>'Gold', 2=>'Silver',
3=>'Bronze') => {1=>"Gold", 2=>"Silver", 3=>"Bronze"}
medal_class[1]
=> "Gold"
medal_class[2]
=> "Silver"
medal_class[3]
=> "Bronze"
medal_class[4]
=> "generic"
medal_class[5]
=> "generic"
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@AgileConsultingLLC.com
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.