<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DZone Snippets: Noodlesinmysandals's Code Snippets</title>
    <link>http://snippets.dzone.com/posts</link>
    <pubDate>Sun, 07 Sep 2008 01:22:42 GMT</pubDate>
    <description>DZone Snippets: Noodlesinmysandals's Code Snippets</description>
    <item>
      <title>Highlight link for current action</title>
      <link>http://snippets.dzone.com/posts/show/2016</link>
      <description>It's very common to want to highlight the current link within a navigation list for the current action being performed.&lt;br /&gt;&lt;br /&gt;What follows is an extremely simplistic approach to solving this, so build upon it as you wish.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;The first thing to do is add the following code to your application_helper.rb file&lt;br /&gt;&lt;code&gt;&lt;br /&gt;def section_link(name,options)&lt;br /&gt;    if options[:action] == @current_action and options[:controller] == @current_controller&lt;br /&gt;       link_to(name, options, :class =&gt; 'on')&lt;br /&gt;    else&lt;br /&gt;      link_to(name,options)&lt;br /&gt;    end&lt;br /&gt;end&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;The above method always expects an :action key to be passed in with the options hash, so if you're one of those people who just pass in the :controller and not the :action so as to default to whatever method you've set it in your routes.rb file, then this won't work for you.&lt;br /&gt;&lt;br /&gt;Finally put this in you're application.rb file, as it makes the current executing action and controller name available to the helper.&lt;br /&gt;&lt;code&gt;&lt;br /&gt;before_filter :instantiate_controller_and_action_names&lt;br /&gt;&lt;br /&gt;def instantiate_controller_and_action_names&lt;br /&gt;      @current_action = action_name&lt;br /&gt;      @current_controller = controller_name&lt;br /&gt;end &lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;In your view use &lt;br /&gt;&lt;code&gt;&lt;br /&gt;&lt;%=section_link('Home',:controller =&gt; 'articles', :action =&gt; 'index')%&gt;&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;Make sure you have a CSS class called "on" or edit the above code. The following CSS is just an example.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;a.on{&lt;br /&gt;  border-bottom: solid 2px #000;&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt; &lt;br /&gt;</description>
      <pubDate>Sun, 14 May 2006 21:33:59 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/2016</guid>
      <author>noodlesinmysandals (Jonathan Conway)</author>
    </item>
  </channel>
</rss>
