views:

73

answers:

2

I have some static pages in a navigation menu. I want to add a class like "current" to the item which is currently displaying.

The way I am doing so is to add tons of helper methods (each for one item) to check the controller and action.

def current_root_class
  'class="current"' if controller_name == "homepage" && action_name == "index" 
end

<ul>
  <li <%= current_root_class %>><%= link_to "Home", root_path %>

Is there any better way to do so!? My current way is so stupid......

A: 

Not truly an answer here, because I'm using quite the same way as you are. I've just defined helper methods to test for multiple controller or actions:

In application_helper.rb

  def controller?(*controller)
    controller.include?(params[:controller])
  end

  def action?(*action)
    action.include?(params[:action])
  end

Then you can use if controller?("homepage") && action?("index", "show") in your views or other helper methods…

Yannis
Your way suit best when there are some pages handled by different controller / actions but in the same menu item, right? Have you encountered more advantages~?
PeterWong
No more advantage except the conciseness of the syntax. I'm curious to see if someone else has a better solution.
Yannis
+1  A: 

I use an awesome gem called Tabs on Rails.

Violet
Thanks for the suggestion. Since my nav is so simple and there's only one with little items, the gem would probably over-powered.
PeterWong