Project

nav_links

0.0
No release in over 3 years
Low commit activity in last 3 years
Rails helper to generate navigation links with a selected class.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

~> 1.3
>= 0

Runtime

< 6.0, >= 3.2.13
 Project Readme

Behold, the NavLinks:

Build Status Gem Version

Gem. Rails. Nav links. The nav_link_to 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. Just replace link_to with nav_link_to in your templates.

Installation

Add this line to your application's Gemfile:

gem 'nav_links'

And then execute:

$ bundle

Usage

The nav_link_to helper is used just like the good old link_to rails helper:

<%= nav_link_to '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

Usage with blocks:

Same usage as link_to:

<%= nav_link_to 'http://example.com/page' do %>
  <strong>My Page</strong>
<% end %>

Group links with same options

You can reduce duplication and wrap nav_link_to calls with the same options with a call to nav_links like this:

<ul class="nav">
  <% nav_links url_segment: 1, wrapper: 'li', selected_class:'active' do |nav| %>
    <%= nav.link_to "Projects", projects_path %>
    <%= nav.link_to "People", people_path %>
    <%= nav.link_to "About", about_path %>
  <% end %>
</ul>

Rainy day?

Here's what you can do on a rainy day:

  • Write integration tests
  • Extend docs

Contributing

  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request

Credits

This gem is mostly based on the gem nav_lynx and sponsored by Sophisticates GmbH