# Selected menu item in the layout

**URL:** <https://www.ruby-forum.com/t/selected-menu-item-in-the-layout/104995>\
**Category:** Rails\
**Created:** [May 24, 2007, 6:20pm UTC](https://www.ruby-forum.com/t/selected-menu-item-in-the-layout/104995 "2007-05-24T18:20:27Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![damaris](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/2bfe46/32.png) [@damaris](https://www.ruby-forum.com/u/damaris)\
**Post date:** [May 24, 2007, 6:20pm UTC](https://www.ruby-forum.com/t/selected-menu-item-in-the-layout/104995/1 "2007-05-24T18:20:27Z")

</div>

Hi you all,

I have a basic menu in my index page which is always shown (Home-New  
page-List pages, etc.)  
This menu list is therefore on the layout of the app. However, I would  
like to change the color (the class from the CSS) of any item when this  
is the current selected item.  
The basic way is asking for the â€œcontroller.action\_nameâ€ and make a  
biiig â€œifâ€ sentence to manage the possibilities:  
If controller.action\_name=â€new\_pageâ€ then

- Home
- New page
- List pages
Else If...

Is there any other short way?  
Thanks

---

<div class="post-metadata">

**Author:** ![damaris](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/2bfe46/32.png) [@damaris](https://www.ruby-forum.com/u/damaris)\
**Post date:** [May 24, 2007, 9:10pm UTC](https://www.ruby-forum.com/t/selected-menu-item-in-the-layout/104995/2 "2007-05-24T21:10:08Z")

</div>

Well you can use link\_to\_unless\_current, but you won’t get  
class=“selected”. Instead you’ll have to code your CSS based on the  
existence of a link or not.

On May 24, 10:20 am, Damaris F. \<rails-mailing-l…@andreas-

---

<div class="post-metadata">

**Author:** ![damaris](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/2bfe46/32.png) [@damaris](https://www.ruby-forum.com/u/damaris)\
**Post date:** [May 25, 2007, 1:25am UTC](https://www.ruby-forum.com/t/selected-menu-item-in-the-layout/104995/3 "2007-05-25T01:25:06Z")

</div>

dasil003 wrote:

> Well you can use link\_to\_unless\_current, but you won’t get  
> class=“selected”. Instead you’ll have to code your CSS based on the  
> existence of a link or not.
> 
> On May 24, 10:20 am, Damaris F. \<rails-mailing-l…@andreas-

Thanks! That did work! and just with one or two changes in my CSS 😃

---

<div class="post-metadata">

**Author:** ![damaris](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/2bfe46/32.png) [@damaris](https://www.ruby-forum.com/u/damaris)\
**Post date:** [May 27, 2007, 10:32pm UTC](https://www.ruby-forum.com/t/selected-menu-item-in-the-layout/104995/4 "2007-05-27T22:32:02Z")

</div>

Damaris F. wrote:

> dasil003 wrote:
> 
> > Well you can use link\_to\_unless\_current, but you won’t get  
> > class=“selected”. Instead you’ll have to code your CSS based on the  
> > existence of a link or not.
> > 
> > On May 24, 10:20 am, Damaris F. \<rails-mailing-l…@andreas-
> 
> Thanks! That did work! and just with one or two changes in my CSS 😃

Um, the thing is that, apart from this menu (I have it at the top, as  
tabs), in each option I can have a submenu (a set of links at the left).  
In this case, when I select one of this suboptions, the menu above get  
unselected… :-S

---

<div class="post-metadata">

**Author:** ![damaris](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/2bfe46/32.png) [@damaris](https://www.ruby-forum.com/u/damaris)\
**Post date:** [May 30, 2007, 12:55am UTC](https://www.ruby-forum.com/t/selected-menu-item-in-the-layout/104995/5 "2007-05-30T00:55:57Z")

</div>

Right well, in that case you need to do something more robust anyway  
because you’ll want the tab to be an active link when you are on any  
of the subpages.

However, you can create a helper that will emulate  
link\_to\_unless\_current with your own semantics so your template  
doesn’t get bloated.

On May 27, 2:32 pm, Damaris F. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
