Skip to content

Instantly share code, notes, and snippets.

@lalitlogical
Created July 4, 2017 10:11
Show Gist options
  • Save lalitlogical/8cb6dca6fe250e61887c8cbf78090e3d to your computer and use it in GitHub Desktop.
Save lalitlogical/8cb6dca6fe250e61887c8cbf78090e3d to your computer and use it in GitHub Desktop.
Active proper menu in Rails views
...
<div class="hor-menu ">
<ul class="nav navbar-nav">
<li class="<%= active('dashboard', 'index')%>">
<%=link_to "Dashboard", root_path %>
</li>
<li class="<%= active('organisations,organisation_people')%>">
<%=link_to "Organisations", organisations_path %>
</li>
<li class="<%= active('people')%>">
<%=link_to "People", people_path %>
</li>
<li class="<%= active('dashboard', 'about')%>">
<%=link_to "About", about_path %>
</li>
<li class="<%= active('dashboard', 'contact')%>">
<%=link_to "Contact", contact_path %>
</li>
</ul>
</div>
....
def active controllers, action_names = nil
class_name = controllers.split(",").any? { |c| controller.controller_name == c.strip } ? "active" : ""
if class_name.present? && action_names.present?
return action_names.split(",").any? { |an| controller.action_name == an.strip } ? "active" : ""
end
class_name
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment