# Navigation Helper

**URL:** <https://www.ruby-forum.com/t/navigation-helper/89193>\
**Category:** Rails\
**Created:** [January 4, 2006, 2:24am UTC](https://www.ruby-forum.com/t/navigation-helper/89193 "2006-01-04T02:24:46Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![DSAndrew\_SDaveSS\_Th](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/3da27b/32.png) [@DSAndrew\_SDaveSS\_Th](https://www.ruby-forum.com/u/DSAndrew_SDaveSS_Th)\
**Post date:** [January 4, 2006, 2:24am UTC](https://www.ruby-forum.com/t/navigation-helper/89193/1 "2006-01-04T02:24:46Z")

</div>

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

```auto
D'Andrew "Dave" Thompson
http://dathompson.blogspot.com
```

---

<div class="post-metadata">

**Author:** ![DSAndrew\_SDaveSS\_Th](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/3da27b/32.png) [@DSAndrew\_SDaveSS\_Th](https://www.ruby-forum.com/u/DSAndrew_SDaveSS_Th)\
**Post date:** [January 4, 2006, 2:41am UTC](https://www.ruby-forum.com/t/navigation-helper/89193/2 "2006-01-04T02:41:02Z")

</div>

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](http://api.rubyonrails.com/classes/ActionView/Helpers/UrlHelper.html#M000336)

---

<div class="post-metadata">

**Author:** ![DSAndrew\_SDaveSS\_Th](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/3da27b/32.png) [@DSAndrew\_SDaveSS\_Th](https://www.ruby-forum.com/u/DSAndrew_SDaveSS_Th)\
**Post date:** [January 4, 2006, 2:59am UTC](https://www.ruby-forum.com/t/navigation-helper/89193/3 "2006-01-04T02:59:55Z")

</div>

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 = ‘[![](https://www.ruby-forum.com/images/home.gif)](https://www.ruby-forum.com/store/home)’  
else  
@home\_link = ‘[\<img  
src=“/images/home.gif”](https://www.ruby-forum.com/store/home/)’  
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](http://api.rubyonrails.com/classes/ActionView/Helpers/UrlHelper.html#M000336)
