Skip to content

Instantly share code, notes, and snippets.

@greypants
Created April 9, 2012 15:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save greypants/2344171 to your computer and use it in GitHub Desktop.
Save greypants/2344171 to your computer and use it in GitHub Desktop.
Rails: Versatile nav_link helper for adding 'selected' class to navigation elements

The nav_link helper

Drop nav_link_helper.rb into app/helpers in your Rails app and enjoy.

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.

###Basic Example If the url of the current page matches the url of the link (http://example.com/page),

<%= nav_link 'My_Page', 'http://example.com/page' %>

Ouputs:

<a href="http://example.com/page" class="selected"></a>

###Options

:selected_class => 'custom-selected-name'

Overrides the default class of ‘selected’ as the class to be added to your selected nav.

:ignore_params => true/false

Set this to true if you want the helper to ignore query strings in the url when comparing. the urls example.com/ and example.com/?param=something will be treated as equal

:controller_segment => numerical_value

Instead of comparing urls, you can compare the controllers of the current page and of the page you’re linking to. Assign a numerical value identifying the controller segment you wish to match. For example, if a page’s or link’s controller is if your controller is ‘members/pages’, and you specify :controller_segment =>2, the helper will look to match */pages

:url_segment => numerical_value

Instead of comparing full urls, you can just segments of the urls. In the path /news/article, 'news' is segment 1, article is segment 2. This is especially useful for category navigation. Assign a numerical value identifying the url segment you wish to match. For example, if a page’s or link’s url is ‘example.com/news/story’, and you specify :url_segment =>1, the helper will look to match /news/*

:wrapper => 'div/li/span/etc...'

Often times you don’t want your ‘selected’ class directly on the anchor tag. You can wrap your anchor tag in another element by specifying :wrapper => ‘li’ (or any other html element). The ‘selected’ class will be added to this wrapper instead of the anchor. Any html_options will still be added directly to the anchor tag.

:wrapper_class => 'additional wrapper class-names'

If you want to specify additional classes for your wrapper (whether it is selected or not), you can add them with :wrapper_class => ‘class-name class-name-2’

###Additional Examples: #####With a Wrapper: <%= nav_link 'Page', some_page_path, {:class => 'link'}, {:wrapper => 'li', :wrapper_class => 'test-wrapper', :selected_class => 'active'} %>

Ouputs:

<li class="active test-wrapper"><a href="example.com/page" class="link">Page</a></li>

#####Ignoring Query Strings: Same as above, but ignoring query strings (example.com/page == example.com/page?query=true&anotherVar=false)

<%= nav_link 'Page', some_page_path, {}, {:ignore_params => 'true'} %>

#####Url Segment Matching: If a specified url segment matches the current url segment (/path/segment_2/page_1 == /path/segment_2/page_99)

<%= nav_link 'Page', some_page_path, {}, {:url_segment => '2'} %>

#####Controller Segment Matching: If a specified controller name segment matches the current controller name segment (/controller_1/controller_2/controller_3 == /controller_1/controller_2/controller_98) -->

<%= nav_link 'Page', some_page_path, {}, {:controller_segment => '2'} %>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment