Skip to content

Instantly share code, notes, and snippets.

@bencates
Created July 26, 2013 21:46
Show Gist options
  • Save bencates/6092496 to your computer and use it in GitHub Desktop.
Save bencates/6092496 to your computer and use it in GitHub Desktop.
Various methods of adding the 'active' class to a nav menu item
module NavbarHelper
# based on url path
def nav_menu_link_1(name, url)
active = request.path.starts_with?(url)
content_tag :li, class: active ? 'active' : nil do
link_to name, url
end
end
# based on controller
def nav_menu_link_2(resource, name = nil)
resource = resource.to_s
name ||= resource.humanize
active = (params[:controller] == resource)
content_tag :li, class: active ? 'active' : nil do
link_to name, send("#{resource}_path")
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment