# Navbar: what's the "Rails Way"?

**URL:** <https://www.ruby-forum.com/t/navbar-whats-the-rails-way/144263>\
**Category:** Rails\
**Created:** [July 27, 2008, 10:21pm UTC](https://www.ruby-forum.com/t/navbar-whats-the-rails-way/144263 "2008-07-27T22:21:40Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [July 27, 2008, 10:21pm UTC](https://www.ruby-forum.com/t/navbar-whats-the-rails-way/144263/1 "2008-07-27T22:21:40Z")

</div>

Hi all.

I’ve the following navbar on my site:

\<divÂ id=“nav”\>  
Â Â Â Â

  
Â Â Â Â Â Â Â Â - \<%= link\_to “Home”, home\_url  
%\>
  
Â Â Â Â Â Â Â Â - \<%= link\_to “Link 1”, link1\_url %\>
  
Â Â Â Â Â Â Â Â - \<%= link\_to “Link 2”, link2\_url %\>
  
Â Â Â Â Â Â Â Â - \<%= link\_to “Link 3”, link3\_url %\>
  
Â Â Â Â Â Â Â Â - \<%= link\_to “Link 4”, link4\_url %\>
  
Â Â Â Â Â Â Â Â - \<%= link\_to “Link 5”, link5\_url %\>
  
Â Â Â Â 
Â Â Â Â Â Â Â Â

My question is: what’s the best approach against id=“current”? I mean:  
I want to my current controller to be the current “selected” item.

I was thinking about use something like my.url/?current=item\_name and  
an helper. So:

#app/helpers/home\_helper.rb  
def output\_li(name, current, &content)  
Â output = “\<li”  
Â output = (name.to\_upper == current.to\_upper) ? ’ id=“current”’ : ‘’  
Â output = “\>”

Â output = yield

Â output = “”  
end

#app/views/layouts

Â Â Â Â 
Â Â Â Â \<%= output\_li "home", params[:current] { link\_to "Home", home\_url(:current="home") } %\> Â Â Â Â \<%= output\_li "link1", params[:current] { link\_to "Link 1", link1\_url(:current="link1") } %\> Â Â Â Â \<%= output\_li "link2", params[:current] { link\_to "Link 2", link2\_url(:current="link2") } %\> Â Â Â Â \<%= output\_li "link3", params[:current] { link\_to "Link 3", link3\_url(:current="link3") } %\> Â Â Â Â \<%= output\_li "link4", params[:current] { link\_to "Link 4", link4\_url(:current="link4") } %\> Â Â Â Â \<%= output\_li "link5", params[:current] { link\_to "Link 5", link5\_url(:current="link5") } %\> Â Â Â Â 

What about my solution? I believe that there is an best approach than  
it, since I unfamiliar with “view logic”.

Sorry for my poor English.

Best regards,

davi vidal

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [July 27, 2008, 10:39pm UTC](https://www.ruby-forum.com/t/navbar-whats-the-rails-way/144263/2 "2008-07-27T22:39:14Z")

</div>

removed\_email\_address@domain.invalid wrote:

> ```
> <li><%= link_to "Link 5", link5_url %></li>
> </ul>               
> 
> ```

Start with link\_to\_unless\_current. If you rewrite your solution using  
it, most  
of your logic might disappear inside.

Then, I’m going to nit-pick some common irritations in your solution, so  
even if  
you rewrite it we will all learn a little.

> def output\_li(name, current, &content)  
> output = “\<li”  
> output = (name.to\_upper == current.to\_upper) ? ’ id=“current”’ : ‘’  
> output = “\>”
> 
> output = yield
> 
> output = “”  
> end

If you must generate HTML, use content\_tag (or Builder::XmlMarkup).

> ```
> <%= output_li "home", params[:current] { link_to "Home",
> 
> ```
> 
> home\_url(:current=“home”) } %\>

params is visible inside your helper, so you don’t need to pass it in.  
Next, the  
{} block is passing to params[:current], which ignores it. You were  
missing a comma.

Finally, your solution duplications “link1” etc. Roll such things up  
into a loop.

Next, a chain of \<%= x %\>\<%= x %\>\<%= x %\> is really a single \<%= x+x+x  
%\>, so if  
you find yourself writing so many \<%= %\> next time you can often clean  
them up, too.

Finally, don’t use an ‘id’ for a ‘class’. Some CSS tools do that (and  
some CSS  
tool users do it!). IDs must not duplicate in a page, and you are always  
a  
refactor away from that!

–  
Phlip

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [July 27, 2008, 11:17pm UTC](https://www.ruby-forum.com/t/navbar-whats-the-rails-way/144263/3 "2008-07-27T23:17:16Z")

</div>

Citando P. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> > ```
> > <li><%= link_to "Link 2", link2_url %></li>
> > <li><%= link_to "Link 3", link3_url %></li>
> > <li><%= link_to "Link 4", link4_url %></li>
> > <li><%= link_to "Link 5", link5_url %></li>
> > </ul>
> > 
> > ```
> 
> Start with link\_to\_unless\_current. If you rewrite your solution  
> using it, most  
> of your logic might disappear inside.

Thank you very much for your response, Phlip. And thank you very much  
for the tip. It’s VERY useful.

> > output = “”  
> > end
> 
> If you must generate HTML, use content\_tag (or Builder::XmlMarkup).

Thanks again.

> > ```
> > <%= output_li "home", params[:current] { link_to "Home",
> > 
> > ```
> > 
> > home\_url(:current=“home”) } %\>
> 
> params is visible inside your helper, so you don’t need to pass it  
> in. Next, the  
> {} block is passing to params[:current], which ignores it. You were  
> missing a comma.

Yeah. Typo. Sorry.

> Finally, your solution duplications “link1” etc. Roll such things up  
> into a loop.

I was using “linkN” as sample.

So, here is what am I doing right now:

#app/views/layouts/post.html.erb

 \<%= build\_navbar %w(home, posts, articles, photos) %\> 

#app/helpers/home\_helper.rb  
module HomeHelper  
def build\_navbar(items)  
items.each do |item|  
content\_tag :li, :class =\> is\_current?(item) do  
link\_to\_unless\_current item.capitalize, item.singularize +  
“\_url”( :current =\> item )  
end  
end  
end

def is\_current?(item)  
(item == params[:current]) ? “current” : nil  
end  
end

I know that, by convention, methods ending by ‘?’ returns true or  
false. But I think that it will increase readbility, right?

Sorry for my very poor English.

Thanks,

davi vidal

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [July 28, 2008, 4:54pm UTC](https://www.ruby-forum.com/t/navbar-whats-the-rails-way/144263/4 "2008-07-28T16:54:53Z")

</div>

OK. My problem is solved. Thank you very much.

What I’m doing, right now, is using an helper and content\_tag.

#app/helpers/home\_helper.rb  
module HomeHelper  
def build\_navbar(items)  
# If I reached the site right now, params[:current]  
# is null.  
params[:current] = “home” if params[:current].nil?

```
menu=""

items.each do |item|
  menu += content_tag :li, :class => is_current?(item) do
    link_to item, send( "#{item}_url", { :current => item } )
  end
end

return menu

```

end

# Methods ending by ‘?’ returns either “true” or “false”

# But using it here in “other” context increases

# readbility.

def is\_current?(item)  
(item == params[:current]) ? “current” : nil  
end  
end

#app/views/layouts/post.html.erb  
[…]

 \<%= build\_navbar %w(home posts articles photos) %\> 

[...]
## Thank you very much.

## Davi V.

E-mail: removed\_email\_address@domain.invalid  
MSN : removed\_email\_address@domain.invalid  
GTalk : removed\_email\_address@domain.invalid  
Skype : davi vidal  
YIM : davi\_vidal  
ICQ : 138815296
