# Top 3 Style

**URL:** <https://www.ruby-forum.com/t/top-3-style/134938>\
**Category:** Rails\
**Created:** [April 16, 2008, 12:11pm UTC](https://www.ruby-forum.com/t/top-3-style/134938 "2008-04-16T12:11:44Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![3quid](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/3/f08c70/32.png) [@3quid](https://www.ruby-forum.com/u/3quid)\
**Post date:** [April 16, 2008, 12:11pm UTC](https://www.ruby-forum.com/t/top-3-style/134938/1 "2008-04-16T12:11:44Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![3quid](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/3/f08c70/32.png) [@3quid](https://www.ruby-forum.com/u/3quid)\
**Post date:** [April 16, 2008, 12:20pm UTC](https://www.ruby-forum.com/t/top-3-style/134938/2 "2008-04-16T12:20:13Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![3quid](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/3/f08c70/32.png) [@3quid](https://www.ruby-forum.com/u/3quid)\
**Post date:** [April 16, 2008, 5:32pm UTC](https://www.ruby-forum.com/t/top-3-style/134938/3 "2008-04-16T17:32:40Z")

</div>

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. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![3quid](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/3/f08c70/32.png) [@3quid](https://www.ruby-forum.com/u/3quid)\
**Post date:** [April 16, 2008, 4:00pm UTC](https://www.ruby-forum.com/t/top-3-style/134938/4 "2008-04-16T16:00:02Z")

</div>

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](http://agileconsultingllc.com)  
removed\_email\_address@domain.invalid
