Skip to content

Instantly share code, notes, and snippets.

@th3james
Created May 17, 2011 13:17
Show Gist options
  • Save th3james/976444 to your computer and use it in GitHub Desktop.
Save th3james/976444 to your computer and use it in GitHub Desktop.
Active link style helper
#returns 'active' if current page == uri
#depth specifies how much of the URL is compared, either [:controller, :action, :complete]
def class_for_link(uri, depth=:action)
path_hash = ActionController::Routing::Routes.recognize_path(uri, { :method => :get })
match = case depth
when :controller
path_hash[:controller] == controller.controller_name
when :complete
uri == request.path
else
[path_hash[:controller], path_hash[:action]] == [controller.controller_name, controller.action_name]
end
match ? 'active' : ''
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment