# Applying a class/id to current link in Haml

**URL:** <https://www.ruby-forum.com/t/applying-a-class-id-to-current-link-in-haml/139256>\
**Category:** Rails\
**Created:** [June 2, 2008, 10:41am UTC](https://www.ruby-forum.com/t/applying-a-class-id-to-current-link-in-haml/139256 "2008-06-02T10:41:24Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![davidtl](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/35a633/32.png) [@davidtl](https://www.ruby-forum.com/u/davidtl)\
**Post date:** [June 2, 2008, 10:41am UTC](https://www.ruby-forum.com/t/applying-a-class-id-to-current-link-in-haml/139256/1 "2008-06-02T10:41:24Z")

</div>

I’ve got a tabbed menu. It currently looks like this in Haml:

%ul#menu  
%li= link\_to\_unless\_current “…”, :controller =\> “…”  
%li= link\_to\_unless\_current “…”, :controller =\> “…”  
%li= link\_to\_unless\_current “…”, :controller =\> “…”  
%li= link\_to\_unless\_current “…”, :controller =\> “…”

I would like one of the

- elements to get an id or class of “current”  
or “selected” or something when it is the current tab.

How can I do that?

Thanks.

---

<div class="post-metadata">

**Author:** ![davidtl](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/35a633/32.png) [@davidtl](https://www.ruby-forum.com/u/davidtl)\
**Post date:** [June 2, 2008, 5:05pm UTC](https://www.ruby-forum.com/t/applying-a-class-id-to-current-link-in-haml/139256/2 "2008-06-02T17:05:13Z")

</div>

David T. wrote:

> I’ve got a tabbed menu. It currently looks like this in Haml:
> 
> %ul#menu  
> %li= link\_to\_unless\_current “…”, :controller =\> “…”  
> %li= link\_to\_unless\_current “…”, :controller =\> “…”  
> %li= link\_to\_unless\_current “…”, :controller =\> “…”  
> %li= link\_to\_unless\_current “…”, :controller =\> “…”
> 
> I would like one of the
> 
> - elements to get an id or class of “current”  
> or “selected” or something when it is the current tab.
> 
> How can I do that?
> 
> Thanks.

Perhaps something like this (untested):

%li{:class =\> ‘myClass’}= blah blah blah

---

<div class="post-metadata">

**Author:** ![davidtl](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/35a633/32.png) [@davidtl](https://www.ruby-forum.com/u/davidtl)\
**Post date:** [June 3, 2008, 8:53am UTC](https://www.ruby-forum.com/t/applying-a-class-id-to-current-link-in-haml/139256/3 "2008-06-03T08:53:12Z")

</div>

Ar Chron wrote:

> > How can I do that?

> Perhaps something like this (untested):
> 
> %li{:class =\> ‘myClass’}= blah blah blah

I found a solution based on your suggestion:

%li{current(“home\_page”)}= bla bla

def current(controller)  
if @current\_controller.eql? controller  
return {:id =\> “current”}  
else  
return {}  
end  
end

@current\_controller is set in the ApplicationController.
