# Navigation menu

**URL:** <https://www.ruby-forum.com/t/navigation-menu/50395>\
**Category:** Rails\
**Created:** [December 15, 2005, 11:27pm UTC](https://www.ruby-forum.com/t/navigation-menu/50395 "2005-12-15T23:27:10Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![lieven](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/c37758/32.png) [@lieven](https://www.ruby-forum.com/u/lieven)\
**Post date:** [December 15, 2005, 11:27pm UTC](https://www.ruby-forum.com/t/navigation-menu/50395/1 "2005-12-15T23:27:10Z")

</div>

I have a navigation menu that contains a link per controller in my  
application.  
This menu is the same for all controllers, but I want this behaviour:

Users  
Statistics

and

Users  
Statistics

depending on the page you’re currently on.

I’ve put a \<render :partial =\> “\_menu”\> in the layout/application.rhtml  
file that is used as layout for all views. And in every view map, I then  
define a \_menu.rhtml partial.  
This is of course not flexible enough, since when I want to change some  
name, I have to make changes in all these files.  
What is a better way?

---

<div class="post-metadata">

**Author:** ![lieven](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/c37758/32.png) [@lieven](https://www.ruby-forum.com/u/lieven)\
**Post date:** [December 15, 2005, 11:52pm UTC](https://www.ruby-forum.com/t/navigation-menu/50395/2 "2005-12-15T23:52:31Z")

</div>

Lieven De Keyzer wrote:

> Statistics
> 
> depending on the page you’re currently on.
> 
> I’ve put a \<render :partial =\> “\_menu”\> in the layout/application.rhtml  
> file that is used as layout for all views. And in every view map, I then  
> define a \_menu.rhtml partial.  
> This is of course not flexible enough, since when I want to change some  
> name, I have to make changes in all these files.  
> What is a better way?

I don’t know how you are getting your controller names, but you can use  
a shared partial, and do  
something like:

\<% Dir[‘app/controllers/\*.rb’].each do |filename|  
controller\_name = File.basename filename %\>  
\<% if @controller.controller\_name == controller\_name %\>  
%= controller\_name %\>  
\<% else %\>  
\<%= controller\_name %\>  
\<%end  
end %\>

@controller should be the controller that the request was made on,

Zach

---

<div class="post-metadata">

**Author:** ![lieven](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/c37758/32.png) [@lieven](https://www.ruby-forum.com/u/lieven)\
**Post date:** [December 16, 2005, 12:04am UTC](https://www.ruby-forum.com/t/navigation-menu/50395/3 "2005-12-16T00:04:36Z")

</div>

You may want to try something like this:

\<%= link\_to\_unless\_current(“Users”, :action=\>‘users’) %\>  
\<%= link\_to\_unless\_current(“Statistics”, :action=\>‘statistics’ ) %\>

---

<div class="post-metadata">

**Author:** ![lieven](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/c37758/32.png) [@lieven](https://www.ruby-forum.com/u/lieven)\
**Post date:** [December 21, 2005, 6:05am UTC](https://www.ruby-forum.com/t/navigation-menu/50395/4 "2005-12-21T06:05:38Z")

</div>

Ken B. wrote:

> You may want to try something like this:
> 
> \<%= link\_to\_unless\_current(“Users”, :action=\>‘users’) %\>  
> \<%= link\_to\_unless\_current(“Statistics”, :action=\>‘statistics’ ) %\>

This won’t work for me, because:

I want to have a menu on the page like:

\<%= link\_to ‘Test1’, :controller =\> ‘test’, :action =\> ‘index’ %\>  
\<%= link\_to ‘Test2’, :controller =\> ‘test2’, :action =\> ‘index’ %\>

Then, When I click on the first link, the menu will still be on the  
page, and above the content, there will be some other links, like this:

\<%= link\_to ‘Edit’, :controller =\> ‘test’, :action =\> ‘edit’ %\>  
\<%= link\_to ‘New’, :controller =\> ‘test’, :action =\> ‘new’ %\>

But for all these pages, I still want the menu to not display a link but  
a span for the current controller.

I hope you understood my explanation?

---

<div class="post-metadata">

**Author:** ![lieven](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/c37758/32.png) [@lieven](https://www.ruby-forum.com/u/lieven)\
**Post date:** [December 21, 2005, 3:42pm UTC](https://www.ruby-forum.com/t/navigation-menu/50395/5 "2005-12-21T15:42:53Z")

</div>

On Wed, 21 Dec 2005, Lieven De Keyzer wrote:

> \<%= link\_to ‘Test1’, :controller =\> ‘test’, :action =\> ‘index’ %\>
> 
> I hope you understood my explanation?

Look up information on link\_to\_if

Something similar to this might work for you:

\<%= link\_to\_if @controller!=‘test’, ‘Test1’, :controller =\> ‘test’,  
:action =\> ‘foo’ %\>

---

<div class="post-metadata">

**Author:** ![lieven](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/c37758/32.png) [@lieven](https://www.ruby-forum.com/u/lieven)\
**Post date:** [December 21, 2005, 6:18pm UTC](https://www.ruby-forum.com/t/navigation-menu/50395/6 "2005-12-21T18:18:21Z")

</div>

Ken B. wrote:

> Look up information on link\_to\_if
> 
> Something similar to this might work for you:
> 
> \<%= link\_to\_if @controller!=‘test’, ‘Test1’, :controller =\> ‘test’,  
> :action =\> ‘foo’ %\>

Indeed,

 \<% controller\_name = @controller.controller\_name %\> - \<%= link\_to\_if controller\_name != 'test', 'Test', :controller =\> 'test', :action =\> 'index' %\> 

did the trick.  
One more question, I used the temporary variable controller\_name because  
I will need it in a lot of links. Should it be a regular variable or an  
instance variable?

---

<div class="post-metadata">

**Author:** ![lieven](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/c37758/32.png) [@lieven](https://www.ruby-forum.com/u/lieven)\
**Post date:** [December 21, 2005, 6:00am UTC](https://www.ruby-forum.com/t/navigation-menu/50395/7 "2005-12-21T06:00:01Z")

</div>

zdennis wrote:

> I don’t know how you are getting your controller names, but you can use  
> a shared partial, and do  
> something like:
> 
> \<% Dir[‘app/controllers/\*.rb’].each do |filename|  
> controller\_name = File.basename filename %\>  
> \<% if @controller.controller\_name == controller\_name %\>  
> %= controller\_name %\>  
> \<% else %\>  
> \<%= controller\_name %\>  
> \<%end  
> end %\>
> 
> @controller should be the controller that the request was made on,
> 
> Zach

But I might not want to create a link for each controller.
