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……
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
Then you can use
if controller?("homepage") && action?("index", "show")in your views or other helper methods…