Navigation Helper

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):

  • <%= link_to 'desktop', {:action => 'index', :controller =>'desktop'}, :id => 'current' %>
  • <%= link_to 'events', {:action => 'index', :controller =>'events'} %>
  • <%= link_to 'financials', {:action => 'index', :controller =>'financials'} %>
  • <%= link_to 'records', {:action => 'index', :controller =>'records'} %>

And the nav css:

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

#navlist
{
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
http://dathompson.blogspot.com

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.

http://api.rubyonrails.com/classes/ActionView/Helpers/UrlHelper.html#M000336

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 = ‘
else
@home_link = ‘<img
src=“/images/home.gif”

end
# 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 O. 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.

Peak Obsession