Forum: Ruby on Rails Navigation Helper

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
C4edcb52652d2b17fe2b38ef13bae729?d=identicon&s=25 D'Andrew "Dave" Thompson (Guest)
on 2006-01-04 02:24
(Received via mailing list)
I am attempting to integrate a main navigation section into my web
application. Naturally the easiest and DRY way to do this would be to
either include it in a layout, or as a partial. The only problem is
that I want the link for the current page to have its own css id
("current").  The only way I can think to accomplish this is to just
include the navigation in every page, but this is certianly not clean.
Any ideas?

Here is the navigation div (varied from List-O-Matic):

<div id="navcontainer">
   <ul id="navlist">
     <li><%= link_to 'desktop',
 				 {:action => 'index', :controller =>'desktop'},
 				  :id => 'current' %></li>
     <li><%= link_to 'events',
 				 {:action => 'index', :controller =>'events'} %></li>
     <li><%= link_to 'financials',
 				 {:action => 'index', :controller =>'financials'} %></li>
     <li><%= link_to 'records',
 				 {:action => 'index', :controller =>'records'} %></li>

And the nav css:

margin: 0;
padding: 0;
height: 22px;
width: 100%;
list-style-type: none;
background: #fff;

 margin: 5px 0px 0px 0px;
 padding: 0 0 20px 5px;
 border-bottom: 1px solid #333;

 #navlist ul, #navlist li
 margin: 0;
 padding: 0;
 display: inline;
 list-style-type: none;

 #navlist a:link, #navlist a:visited
 float: left;
 line-height: 14px;
 font-weight: bold;
 margin: 0 10px 4px 5px;
 text-decoration: none;
 color: #999;

 #navlist a:link#current, #navlist a:visited#current, #navlist a:hover
 border-bottom: 4px solid #333;
 padding-bottom: 2px;
 background: transparent;
 color: #333;

Thanks, Dave
D'Andrew "Dave" Thompson
C8a634a01a2c4508360874bff7fb1a7f?d=identicon&s=25 Kevin Olbrich (Guest)
on 2006-01-04 02:41
take a look at the description for 'link_to_unless_current'.

If necessary, you could over-ride this and have it use a different class
or id for the link.
73606b1d1042de19056f3d59492b652f?d=identicon&s=25 charlie bowman (Guest)
on 2006-01-04 02:59
Another method (one I used yesterday actually) is to create a helper.
Have each template create a varable. <% @current_page = :name_of_page %>

In the layout replace your navbar with  <%= navigation_bar %>

create a helper method in application_helper.rb like this

def navigation_bar
    if @current_page == :home
      @home_link = '<a href="/store/home" class="normal"><img
      @home_link = '<a href="/store/home/" class="rollover"><img
    # do this if/else for each link and then combine them into one
variable at the end.  This variable will be written when you call
navigation_bar in your layout.

This method worked great for me.  I'm sure there is a better way, but I
asked this here about 3 days ago and got no response.

Kevin Olbrich wrote:
> take a look at the description for 'link_to_unless_current'.
> If necessary, you could over-ride this and have it use a different class
> or id for the link.
This topic is locked and can not be replied to.