Navigation menu


#1

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?


#2

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


#3

You may want to try something like this:

<%= link_to_unless_current(“Users”, :action=>‘users’) %>
<%= link_to_unless_current(“Statistics”, :action=>‘statistics’ ) %>


#4

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?


#5

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’ %>


#6

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?


#7

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.