This helper has finally been moved into a gem called nav_lynx!
https://github.com/vigetlabs/nav_lynx
http://rubygems.org/gems/nav_lynx
Thanks to @brianjlandau and @reagent for getting that set up and tested!
The nav_link helper works just like the standard Rails link_to helper, but adds a 'selected' class to your link (or its wrapper) if certain criteria are met. By default, if the link's destination url is the same url as the url of the current page, a default class of 'selected' is added to the link.
<%= nav_link 'My Page', my_path %>
When my_path
is the same as the current page url, this outputs:
<a class="selected" href="http://example.com/page">My Page</a>
For more options and full usage details, see: http://viget.com/extend/rails-selected-nav-link-helper
Drop nav_link_helper.rb into app/helpers in your Rails 3.x app and enjoy.
Same usage as link_to
:
<%= nav_link 'http://example.com/page' do %>
<strong>My Page</strong>
<% end %>
This is useful. I have a couple of suggestions, though:
First, please copy the usage instructions from
http://viget.com/extend/rails-selected-nav-link-helper to README.markdown. You
never know when and if the article will become unavailable for some reason, and
it's a good idea to keep everything to do with one gist in one place.
Second, it would be nice if one could also give an option for defining a class
name to be given for unselected links, in case the user wants to define
properties for such links in a .css file.
Third, I didn't really see the point of methods that are called from only one
place in the code -- I think they can confuse rather than clarifying the code,
so I merged the code of methods link_classes, html_options and link to method
LinkGenerator::to_html.
I also made a couple of changes to parameter and variable names, for better
self-documentation and more clarity. These are the resulting changes to the
code (tested to work):
And this is a usage example, from a .html.erb file: