Skip to content

Instantly share code, notes, and snippets.

@itsprdp
Last active June 23, 2016 07:47
Show Gist options
  • Save itsprdp/41615c96dc1c4336e145586611e02eb7 to your computer and use it in GitHub Desktop.
Save itsprdp/41615c96dc1c4336e145586611e02eb7 to your computer and use it in GitHub Desktop.
CSS active link class method for rails views

Active link CSS class

Usage:

%li{:class => active_class(:c => 'controller_name',:a => ['action1','action2'],:id => @object.id) }
  = link_to some_path do
    %i.fa.fa-chevron-circle-right.fa-fw
    %span Link Name
module ApplicationHelper
# Given the options, this method determines whether the css class 'active' can be applied to the caller in the sidenav.
# options[:c] - controller
# options[:a] - action
# options[:id] - id of an object
def active_class options
active =
(!options[:c] || (options[:c] == params[:controller])) &&
(!options[:a] || (options[:a].include? params[:action])) &&
(!options[:id] || (options[:id].to_s == params[:id]))
active ? 'active' : false
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment