[ActionView] active_link_to Proposal

Hello, I’m writing to propose adding active_link_to to the ActionView UrlHelpers

The active_link_to gem has been a staple in most Rails applications I’ve worked in. In even the most basic applications, highlighting the current page in navigation is something every Rails dev has done over and over.

While the gem has been great, in Rails fashion I would propose a slimmed-down version to help provide for this low-hanging fruit. Seeing current_page?, it seems the intent of this functionality is almost there, but you still end up having to do some gymnastics to achieve the desired result. Existing solutions tend to revolve around monkey-patching or class manipulation via helpers or inline view logic. That, or you grab a gem.

I propose:

  • adding active_link_to to ActionView::Helpers::UrlHelpers it will have the same method signature(s) aslink_to`
  • using active_link_to will append ‘active’ to the link element’s class if the link’s path == the current request path
  • class can be modified with an active_class: 'my_active_class' option

I have already monkey-patched this solution in my own projects, and the new code should be minimal. I’d be happy to open the PR with tests/documentation.

Thank you for your consideration!