# 'link\_to\_class\_current' helper

**URL:** <https://www.ruby-forum.com/t/link-to-class-current-helper/131199>\
**Category:** Rails\
**Created:** [March 6, 2008, 9:26pm UTC](https://www.ruby-forum.com/t/link-to-class-current-helper/131199 "2008-03-06T21:26:12Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![chunkyslink](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/f0a364/32.png) [@chunkyslink](https://www.ruby-forum.com/u/chunkyslink)\
**Post date:** [March 6, 2008, 9:26pm UTC](https://www.ruby-forum.com/t/link-to-class-current-helper/131199/1 "2008-03-06T21:26:12Z")

</div>

I’m trying to create a navigation which adds :class =\> “current” to the  
currently selected nav link (just like the WordPress nav). I presume the  
class would be determined by the controller params (which isn’t a  
problem as all my tabs are named after a controller). If so, how I would  
go about implementing this on the following example;

- \<%= link\_to 'Users', users\_path %\>
- \<%= link\_to 'Posts', posts\_path %\>
- \<%= link\_to 'Pages', page\_path %\>

I made some attempts at Googling this and subsequently came across a  
snippet on dzone, [http://snippets.dzone.com/posts/show/1996](http://snippets.dzone.com/posts/show/1996) , but the  
plugin link appears to be dead.

If only there was a ‘link\_to\_class\_current’ helper!

---

<div class="post-metadata">

**Author:** ![chunkyslink](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/f0a364/32.png) [@chunkyslink](https://www.ruby-forum.com/u/chunkyslink)\
**Post date:** [March 7, 2008, 5:22am UTC](https://www.ruby-forum.com/t/link-to-class-current-helper/131199/2 "2008-03-07T05:22:16Z")

</div>

Neil C. wrote:

> - \<%= link\_to 'Users', users\_path %\>
> - \<%= link\_to 'Posts', posts\_path %\>
> - \<%= link\_to 'Pages', page\_path %\>
> 
> I made some attempts at Googling this and subsequently came across a  
> snippet on dzone, [http://snippets.dzone.com/posts/show/1996](http://snippets.dzone.com/posts/show/1996) , but the  
> plugin link appears to be dead.
> 
> If only there was a ‘link\_to\_class\_current’ helper!

by class =\> “current”, I assuming that you want to add a css class  
attribute to link\_to which happens to be specified within the third  
argument in the method

link\_to ‘Users’, users\_path, :class =\> “current”

If on the other hand, you wanted to pass this class =\> “current” to your  
controller as params then you can do it within the second argument of  
link\_to:

link\_to ‘Users’, {:action =\> ‘index’, :class =\> “current”}

please check [http://api.rubyonrails.org/](http://api.rubyonrails.org/)

hth

ilan

---

<div class="post-metadata">

**Author:** ![chunkyslink](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/f0a364/32.png) [@chunkyslink](https://www.ruby-forum.com/u/chunkyslink)\
**Post date:** [March 7, 2008, 9:11am UTC](https://www.ruby-forum.com/t/link-to-class-current-helper/131199/3 "2008-03-07T09:11:03Z")

</div>

Ilan B. wrote:

> Neil C. wrote:
> 
> > - \<%= link\_to 'Users', users\_path %\>
> > - \<%= link\_to 'Posts', posts\_path %\>
> > - \<%= link\_to 'Pages', page\_path %\>
> > 
> > I made some attempts at Googling this and subsequently came across a  
> > snippet on dzone, [http://snippets.dzone.com/posts/show/1996](http://snippets.dzone.com/posts/show/1996) , but the  
> > plugin link appears to be dead.
> > 
> > If only there was a ‘link\_to\_class\_current’ helper!
> 
> by class =\> “current”, I assuming that you want to add a css class  
> attribute to link\_to which happens to be specified within the third  
> argument in the method
> 
> link\_to ‘Users’, users\_path, :class =\> “current”
> 
> If on the other hand, you wanted to pass this class =\> “current” to your  
> controller as params then you can do it within the second argument of  
> link\_to:
> 
> link\_to ‘Users’, {:action =\> ‘index’, :class =\> “current”}
> 
> please check [http://api.rubyonrails.org/](http://api.rubyonrails.org/)
> 
> hth

Ilan, I wanted to dynamically generate a “current” css class in nav  
links based on the name of the current controller. I’m happy with adding  
css class options to link\_to - I just don’t know how to do this with  
conditional statements. For example, take the following URLs;

/users/  
/users/1  
/users/1/posts (I appreciate this one may complicate things)

For all those URLs, I would expect my nav to generate the following  
navigation (because the UsersController, or UsersPostsController, is in  
use);

- \<%= link\_to 'Users', users\_path, :class =\> "current" %\>
- \<%= link\_to 'Posts', posts\_path %\>
- \<%= link\_to 'Pages', pages\_path %\>

I have RailsBrain in my bookmarks but I haven’t found anything that  
specifically relates to my issue; the link\_to family of methods don’t  
see to cover this. Do you have any further suggestions?

---

<div class="post-metadata">

**Author:** ![chunkyslink](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/f0a364/32.png) [@chunkyslink](https://www.ruby-forum.com/u/chunkyslink)\
**Post date:** [March 7, 2008, 3:07pm UTC](https://www.ruby-forum.com/t/link-to-class-current-helper/131199/4 "2008-03-07T15:07:36Z")

</div>

Neil C. wrote:

> I have RailsBrain in my bookmarks but I haven’t found anything that  
> specifically relates to my issue; the link\_to family of methods don’t  
> see to cover this. Do you have any further suggestions?

Neil,

Ohhh… ok, now I think I understand you, if you want to get the current  
controller in the view, just pass it in…

In your controller: (before sending it off to the view)  
@class = self.class.name

In your view,  
\<%= link\_to ‘Users’, :action =\> “whatever”, :class =\> @class %\>

hth

ilan

---

<div class="post-metadata">

**Author:** ![chunkyslink](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/f0a364/32.png) [@chunkyslink](https://www.ruby-forum.com/u/chunkyslink)\
**Post date:** [March 7, 2008, 3:41pm UTC](https://www.ruby-forum.com/t/link-to-class-current-helper/131199/5 "2008-03-07T15:41:52Z")

</div>

Ilan B. wrote:

> In your controller: (before sending it off to the view)  
> @class = self.class.name
> 
> In your view,  
> \<%= link\_to ‘Users’, :action =\> “whatever”, :class =\> @class %\>
> 
> hth
> 
> ilan

ilan, that puts the controller name as a class in the link, so it  
differentiates the link and will work - but I’d rather the link was  
explicity given the class “current”, because otherwise I’ll need to  
include “UserController, PostController” and every other type of  
controller name, within the stylesheet declaration for the ‘currently  
selected tab’.

Maybe it would help if you looked at my WordPress site:  
[http://dotneil.com](http://dotneil.com). You’ll see that the parent tab is given a  
“current\_page\_item”, so when you hit the homepage, the ‘Blog’ tab is  
highlighted.

I can achieve the same effect in a dirty, non-DRY way using condition  
statements against every single tab, i.e. \<% if params[:controller] ==  
“users” %\>, but, alongside being messy, this creates ambiguities with  
nested resources.

---

<div class="post-metadata">

**Author:** ![chunkyslink](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/f0a364/32.png) [@chunkyslink](https://www.ruby-forum.com/u/chunkyslink)\
**Post date:** [March 8, 2008, 4:34am UTC](https://www.ruby-forum.com/t/link-to-class-current-helper/131199/6 "2008-03-08T04:34:21Z")

</div>

Neil C. wrote:

> I can achieve the same effect in a dirty, non-DRY way using condition  
> statements against every single tab, i.e. \<% if params[:controller] ==  
> “users” %\>, but, alongside being messy, this creates ambiguities with  
> nested resources.

something like this then?

#assuming that the controller was passed in as discussed before  
html\_options = {}  
html\_options.merge(:class =\> ‘current’) if @class ==  
UserController.class.name

\<%= link\_to ‘Users’, users\_path, html\_options %\>

also, check out current\_page? in the rails api…

I know you have been explaining your heart out on this so I truly hope  
this gives you some clues… thanks for being patient… 🙂

ilan

---

<div class="post-metadata">

**Author:** ![chunkyslink](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/f0a364/32.png) [@chunkyslink](https://www.ruby-forum.com/u/chunkyslink)\
**Post date:** [March 8, 2008, 6:16am UTC](https://www.ruby-forum.com/t/link-to-class-current-helper/131199/7 "2008-03-08T06:16:53Z")

</div>

link\_to\_unless\_current supports this behavior quite nicely.

By default, it won’t make a link at all for the current action which can  
be  
quite nice, but the method supports a block that acts as the “unless”  
condition as well.

\<%=link\_to\_unless\_current(“Users”, users\_path) do  
link\_to("Users, :users\_path, {:class=\>:current})  
%\>

Or

\<%=link\_to\_unless\_current(“Users”, users\_path) do  
‘Users’  
%\>

Or however you see fit.

On Fri, Mar 7, 2008 at 9:34 PM, Ilan B.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![chunkyslink](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/f0a364/32.png) [@chunkyslink](https://www.ruby-forum.com/u/chunkyslink)\
**Post date:** [March 8, 2008, 9:57am UTC](https://www.ruby-forum.com/t/link-to-class-current-helper/131199/8 "2008-03-08T09:57:49Z")

</div>

Ilan B. wrote:

> I know you have been explaining your heart out on this so I truly hope  
> this gives you some clues… thanks for being patient… 🙂
> 
> ilan

No need to apologise - you’ve been a great help - and not for the first  
time, either!

‘current\_page?’ may do the trick. I just realised the nav will use funky  
CSS dropdowns to contain more than one link per list item, which  
completely throws a spanner in things;

- \<%= link\_to 'Users', users\_path %\>
- \<%= link\_to 'Posts', posts\_path %\>
- \<%= link\_to 'Pages', pages\_path %\>

I just tried a quick ‘n’ dirty route;

- \<%= link\_to 'Users', users\_path %\>

And I think I could get that working quite nicely if I can work out how  
to return “current” if true, nil if false, and also include multiple  
controller actions, too. Specifying the :controller without an :action  
only returns true if the current\_page is the ‘index’, which baffles me.

This doesn’t work, but if I could do something like it, I’d be all set;

- "\>\<%= link\_to 'Users', users\_path %\>

Brian H. wrote:

> link\_to\_unless\_current supports this behavior quite nicely.
> 
> By default, it won’t make a link at all for the current action which can  
> be  
> quite nice, but the method supports a block that acts as the “unless”  
> condition as well.
> 
> \<%=link\_to\_unless\_current(“Users”, users\_path) do  
> link\_to("Users, :users\_path, {:class=\>:current})  
> %\>

Thanks Brian, the link\_to\_unless\_current trick absolutely hits the  
previous requirements on the head. It’s just a shame that I may need to  
add the class=“current” to the parent element instead. Argh!
