Highlight Link Based on Current Page in Rails
This is common pattern in website navigation, where it highlights the link (usually by setting
class=”active”) that took you to the current page while you are on that page.
First, define a helper:
1 2 3
Then call it in your link_to’s in your layout as such:
1 2 3
This effect is achieved due to how link_to handles being passed
nil for its
:class, so when
nil (because its not the current page),
link_to outputs nothing as its class (not
class=”” as you might expect).