# Help with link\_to and its kin

**URL:** <https://www.ruby-forum.com/t/help-with-link-to-and-its-kin/155871>\
**Category:** Rails\
**Created:** [December 17, 2008, 8:18pm UTC](https://www.ruby-forum.com/t/help-with-link-to-and-its-kin/155871 "2008-12-17T20:18:08Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [December 17, 2008, 8:18pm UTC](https://www.ruby-forum.com/t/help-with-link-to-and-its-kin/155871/1 "2008-12-17T20:18:08Z")

</div>

Hi Everyone

I am a new user of Ruby and Rails, and would like some help with the  
following problem.

I have created a navbar with a styled unordered list and I would like  
to apply class=“current” to the tab for the current page.

Currently I am using a large, cumbersome and ugly if…then block,  
rendering the class=“current” into the html and link\_tos with  
current\_page?.

I am absolutely sure this is a fairly common task for many rails  
developers, so common and easy in fact, that noone even bothers  
mentioning it on the web (a quick reference to the fact that I have  
unsuccessfully googled already).

Help will be rewarded with chunky bacon.  
P&P Not included

Thanks  
Marc

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [December 17, 2008, 8:21pm UTC](https://www.ruby-forum.com/t/help-with-link-to-and-its-kin/155871/2 "2008-12-17T20:21:05Z")

</div>

On 17 Dec 2008, at 17:58, removed\_email\_address@domain.invalid wrote:

> rendering the class=“current” into the html and link\_tos with  
> current\_page?.

I would write a small helper, something like

def magic\_link\_to(name, options={}, html\_options={})  
if current\_page?(options)  
html\_options = html\_options.merge(:class =\> ‘current’)  
end  
link\_to name, options, html\_options  
end

Then just use magic\_link\_to instead of link\_to

Fred

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [December 17, 2008, 9:32pm UTC](https://www.ruby-forum.com/t/help-with-link-to-and-its-kin/155871/3 "2008-12-17T21:32:27Z")

</div>

On Dec 17, 9:20 pm, Frederick C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> > to apply class=“current” to the tab for the current page.  
> > end  
> > link\_to name, options, html\_options  
> > end
> 
> Then just use magic\_link\_to instead of link\_to
> 
> Fred

Thanks for that Fred

Added the magic and its working embarrasingly well. I think I was  
stuck in the cycle of coming up with increasingly complex solutions to  
a simple problem, perhaps its time for some coffee.

Thanks again  
Marc
