Skip to content

Instantly share code, notes, and snippets.

@Xavier-IV
Created June 22, 2017 13:24
Show Gist options
  • Save Xavier-IV/d61c87c673d8a191b99600f00a68105f to your computer and use it in GitHub Desktop.
Save Xavier-IV/d61c87c673d8a191b99600f00a68105f to your computer and use it in GitHub Desktop.
Simple helper for Rails View to handle active class
module ApplicationHelper
# Most commonly used.
# If you have a ROUTE of :admin_home or admin_home_path,
#
# <li <%= set_active_if_path(admin_home_path) %>>
# <%= link_to :admin_home do %>
# <i class="fa fa-home"></i> <span>Dashboard </span>
# <% end %>
# </li>
# --------------------------------------
def set_active_if_path(name)
if current_page?(name)
raw 'class="active"'
end
end
# Use this if you are using a parent-child navigation,\
# where you want to set the parent as active.
# Usually, you can group the whole parent as a controller.
# If you have a CONTROLLER of :company
#
# <li class="treeview <%= set_active_if_controller('company') %>"> <--------This is where the parennt is
# <a href="#">
# <i class="fa fa-building-o"></i>
# <span>Companies</span>
# <span class="pull-right-container">
# <i class="fa fa-angle-left pull-right"></i>
# </span>
# </a>
# <ul class="treeview-menu">
# <li <%= set_active_if_path(:show_company) %>> <--------This is her child
# <%= link_to :show_company do %>
# <i class="fa fa-eye"></i> <span>Show companies</span>
# <% end %>
# </li>
# --------------------------------------
def set_active_if_controller(name)
if controller.controller_name == name
raw 'menu-open active'
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment