Skip to content

Instantly share code, notes, and snippets.

@mynameispj
Created June 2, 2013 00:24
Show Gist options
  • Save mynameispj/5692162 to your computer and use it in GitHub Desktop.
Save mynameispj/5692162 to your computer and use it in GitHub Desktop.
Rails - Easy "active" classes for menu links in Rails
module ApplicationHelper
def current_class?(test_path)
return 'active' if request.path == test_path
''
end
end
<nav class="subnav">
<ul>
<li><%= link_to "account stats", account_path, :class => current_class?(account_path) %></li>
<li><%= link_to "payment information", '/account/payment', :class => current_class?('/account/payment') %></li>
<li><%= link_to "profile settings", profile_path, :class => current_class?(profile_path) %></li>
</ul>
</nav>
@munirdelta
Copy link

Perfect thanks Improved the naming convention tho

module ApplicationHelper
  def active_class_if_url(url)
    return 'active' if request.path == url
    ''
  end
end

@czepesch
Copy link

czepesch commented Feb 16, 2022

guys, how can I do styling of active links if I have links like that in one view:
- @glossaries.each do |g|
%a.{ :href => glossary_path(g), data: { 'turbo_frame': :'entry' } }
= g.title

But result of clicking g.title is rendered in another turbo frame and url in a browser not present.
Even if I force browser to have url in address bar with "data-turbo-action" => "advance" Helpers is doesn't working.

@mynameispj @gnclmorais @munirdelta @dan3lson

thanks in advance!

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