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?