Top 3 Style

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

On 16 Apr 2008, at 11:11, Graham D. 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

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 B. [email protected]

On Apr 16, 2008, at 6:19 AM, Frederick C. wrote:

(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 B. http://agileconsultingllc.com
[email protected]