# How to change the menu depending on the controller/method?

**URL:** <https://www.ruby-forum.com/t/how-to-change-the-menu-depending-on-the-controller-method/83416>\
**Category:** Rails\
**Created:** [January 25, 2007, 9:15pm UTC](https://www.ruby-forum.com/t/how-to-change-the-menu-depending-on-the-controller-method/83416 "2007-01-25T21:15:46Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Matthieu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/da6949/32.png) [@Matthieu](https://www.ruby-forum.com/u/Matthieu)\
**Post date:** [January 25, 2007, 9:15pm UTC](https://www.ruby-forum.com/t/how-to-change-the-menu-depending-on-the-controller-method/83416/1 "2007-01-25T21:15:46Z")

</div>

Hello,

I’m still dtarting with ruby on rails. And I’m trying to have a menu  
depending on the current controller/method.

Here is what I was thinking of (please tell me if there’s a better way  
to do it):

I have a database with menu\_groups (corresponding to controllers) and  
menu\_elements(corresponding to methods). Each menu\_element belongs to  
one menu\_group.

I created a class Menu where I get all the menu\_groups and  
menu\_elements, and where I have the active controller and method.

"  
class Menu  
attr\_reader :menu\_groups  
attr\_reader :menu\_group\_active  
attr\_reader :menu\_elements  
attr\_reader :menu\_element\_active  
def initialize  
current\_url\_elements=request\_uri().split("/")

```
@menu_groups << MenuGroup.find(:name,:order => "id")
@menu_group_active = controller.controller_name
@menu_elements << MenuElement.find(:all,:order => "menu_group_id

```

id")  
@menu\_elements\_active = current\_url\_elements[-1]  
end  
end  
"

Then, thanks to the attributes of this class I’ll be able in my layout  
to make the difference between active and non active groups/elements.

The problem here is that I don’t know how to get the url into a  
variable…

If my method is good, can you please tell me how to make it works?  
If not, can you show me the right way to do this??

Thanks a lot

Best regards,

Matthieu

---

<div class="post-metadata">

**Author:** ![Matthieu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/da6949/32.png) [@Matthieu](https://www.ruby-forum.com/u/Matthieu)\
**Post date:** [January 25, 2007, 9:45pm UTC](https://www.ruby-forum.com/t/how-to-change-the-menu-depending-on-the-controller-method/83416/2 "2007-01-25T21:45:07Z")

</div>

Unless I am missing something…Make layouts out of your menus then do  
something like this

In your controllers or even your application.rb  
layout :select\_layout

then this method at the end of your controller  
private  
def select\_layout  
[‘action\_name1’, ‘action\_name2’].include?(action\_name) ? “layout1”  
: “layout2”  
end

Storing references to a menu in a DB seems wrong.

Help that helps.

---

<div class="post-metadata">

**Author:** ![Matthieu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/da6949/32.png) [@Matthieu](https://www.ruby-forum.com/u/Matthieu)\
**Post date:** [January 25, 2007, 9:18pm UTC](https://www.ruby-forum.com/t/how-to-change-the-menu-depending-on-the-controller-method/83416/3 "2007-01-25T21:18:56Z")

</div>

I add something:

I have one common layout for all my controllers.

---

<div class="post-metadata">

**Author:** ![Matthieu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/da6949/32.png) [@Matthieu](https://www.ruby-forum.com/u/Matthieu)\
**Post date:** [January 25, 2007, 10:08pm UTC](https://www.ruby-forum.com/t/how-to-change-the-menu-depending-on-the-controller-method/83416/4 "2007-01-25T22:08:11Z")

</div>

Kim wrote:

> Make layouts out of your menus

Do you mean, I have to do one layout for every situation my menu will be  
in (=the number of method I’ll have)??  
It seems a lot to me and maybe I’m wrong but it would induce a lot of  
redundency, no?

> In your controllers or even your application.rb  
> layout :select\_layout
> 
> then this method at the end of your controller  
> private  
> def select\_layout  
> [‘action\_name1’, ‘action\_name2’].include?(action\_name) ? “layout1”  
> : “layout2”  
> end

what does ‘action\_name’ represent ?

---

<div class="post-metadata">

**Author:** ![Matthieu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/da6949/32.png) [@Matthieu](https://www.ruby-forum.com/u/Matthieu)\
**Post date:** [January 25, 2007, 10:38pm UTC](https://www.ruby-forum.com/t/how-to-change-the-menu-depending-on-the-controller-method/83416/5 "2007-01-25T22:38:21Z")

</div>

No - you make a layout for the menu - how many menus do you have? Your  
menu should retain a consistant look across most of your  
controllers/actions - unless I am not understanding something. I don’t  
understand what you mean by “the situation my menu will be in (=the  
number of method I’ll have)” If you mean, for example, that you want  
the “comments” menu item to be a different color when you are  
performing that action then use CSS and helper methods like  
link\_to\_unless\_current amongst others.

It seems like you might want to read some ofthe multiple of books this  
list preaches about.

def select\_layout

> > ```
> > ['action_name1', 'action_name2'].include?(action_name) ? "layout1"
> > 
> > ```
> > 
> > : “layout2”  
> > endwhat does ‘action\_name’ represent ?

action\_name is the name of the action that should use layout1.
