Skip to content

Instantly share code, notes, and snippets.

@joshmosh
Created March 26, 2018 17:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joshmosh/cc9f066e59eb2ea7662db4332ad1a62d to your computer and use it in GitHub Desktop.
Save joshmosh/cc9f066e59eb2ea7662db4332ad1a62d to your computer and use it in GitHub Desktop.
Rails Active Link Helper
module ApplicationHelper
def app_link_to(name, url, html_options, active_options)
css_class = html_options[:class].split(' ')
if params[:controller] == active_options[:controller]
css_class << active_options[:active_class]
end
html_options[:class] = css_class.join(' ')
link_to(name, url, html_options)
end
end
<%= app_link_to 'Clients',
clients_path,
{ class: 'navigation__app--link' },
controller: 'clients',
active_class: 'selected'
%>
@joshmosh
Copy link
Author

A simple example of how to extend the build in link_to helper to highlight links in your application. This is not intended to be a drop in solution for every case but it's a good place to start.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment