I have made a small helper that adds class="selected". Primarily it uses current_page? to investigate if the present path is the current menu item, and the select it.
module MenuHelper
#renders menu items and emphasizes current menu item
def topmenu
pages = {
"products" => admin_products_path,
"categories" => admin_categories_path,
"catalogs" => admin_catalogs_path,
"sales channels" => admin_sales_channels_path
}
pages.map do |key, value|
classnames = %( class="current") if current_page?(value)
"<li#{classnames}>#{link_to(key, value)}</li>"
end
end
end
And in /layouts/application.html.erb:
<ul class="topmenu">
<%= topmenu %>
</ul>
There is a big flaw in my approach. Selecting /admin/catalogs works like a charm. But any subpages do not (/admin/catalogs/1, etc.)
I think that my approach may be flawed by the limitations of the current_page? method
do you have any ideas on how I should either enhance this script to accept similar urls, or is there a smarter way to achieve it?
The way I built a similar menu helper was to look at the
controller_nameandaction_nameattributes and then decide if a given page is selected/active or not. That’s less specific than the full URL, so might be useful.