I made a little helper that adds class="selected". First of all, he uses current_page?to investigate whether the current path is the current menu item and select it.
module MenuHelper
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. Choice /admin/catalogsworks like a charm. But there are no subpages ( /admin/catalogs/1etc.)
I think my approach may be corrupted by current_page?method limitations
Do you have any ideas on how I should improve this script to accept similar URLs, or is there a smarter way to achieve it?
source
share