<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Blog &#124; The Working Group &#187; active_link_to</title>
	<atom:link href="http://blog.twg.ca/tag/active_link_to/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.twg.ca</link>
	<description></description>
	<lastBuildDate>Fri, 23 Jul 2010 01:09:02 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Active state for link_to == active_link_to. A solution for building navigation systems in Rails.</title>
		<link>http://blog.twg.ca/2009/11/active-state-for-link_to-active_link_to/</link>
		<comments>http://blog.twg.ca/2009/11/active-state-for-link_to-active_link_to/#comments</comments>
		<pubDate>Fri, 27 Nov 2009 18:07:45 +0000</pubDate>
		<dc:creator>Oleg</dc:creator>
				<category><![CDATA[Tech]]></category>
		<category><![CDATA[active state]]></category>
		<category><![CDATA[active_link]]></category>
		<category><![CDATA[active_link_to]]></category>
		<category><![CDATA[github]]></category>
		<category><![CDATA[navigation]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[ruby]]></category>
		<category><![CDATA[ruby on rails]]></category>

		<guid isPermaLink="false">http://blog.twg.ca/?p=346</guid>
		<description><![CDATA[I have a question for you. How do you deal with the logic of setting links as active in your navs? Actually, I don&#8217;t want to know. It&#8217;s probably awful. I might have a pretty good solution for you. Let&#8217;s take a look at a very simple nav. These examples all use HAML: %ul &#160; [...]]]></description>
			<content:encoded><![CDATA[<p>I have a question for you. How do you deal with the logic of setting links as active in your navs?</p>
<p>Actually, I don&#8217;t want to know. It&#8217;s probably awful. I might have a pretty good solution for you. Let&#8217;s take a look at a very simple nav. These examples all use <a href="http://haml-lang.com/">HAML</a>:</p>
<div class="codecolorer-container ruby default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="ruby codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color:#006600; font-weight:bold;">%</span>ul<br />
&nbsp; <span style="color:#006600; font-weight:bold;">%</span>li= link_to <span style="color:#996600;">'Home'</span>, home_path<br />
&nbsp; <span style="color:#006600; font-weight:bold;">%</span>li= link_to <span style="color:#996600;">'Puppies'</span>, puppies_path<br />
&nbsp; <span style="color:#006600; font-weight:bold;">%</span>li= link_to <span style="color:#996600;">'Kittens'</span>, kittens_path<br />
&nbsp; <span style="color:#006600; font-weight:bold;">%</span>li= link_to <span style="color:#996600;">'Froggies'</span>, froggies_path</div></div>
<p>Right, that&#8217;s great. But how do I insert logic as to what link gets marked as active? To begin, let&#8217;s quickly install this random gem:</p>
<div class="codecolorer-container bash default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="bash codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #c20cb9; font-weight: bold;">sudo</span> gem <span style="color: #c20cb9; font-weight: bold;">install</span> active_link_to</div></div>
<p>And then change our nav a bit:</p>
<div class="codecolorer-container ruby default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="ruby codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color:#006600; font-weight:bold;">%</span>ul<br />
&nbsp; <span style="color:#006600; font-weight:bold;">%</span>li= active_link_to <span style="color:#996600;">'Home'</span>, home_path<br />
&nbsp; <span style="color:#006600; font-weight:bold;">%</span>li= active_link_to <span style="color:#996600;">'Puppies'</span>, puppies_path<br />
&nbsp; <span style="color:#006600; font-weight:bold;">%</span>li= active_link_to <span style="color:#996600;">'Kittens'</span>, kittens_path<br />
&nbsp; <span style="color:#006600; font-weight:bold;">%</span>li= active_link_to <span style="color:#996600;">'Froggies'</span>, froggies_path</div></div>
<p>And that&#8217;s pretty much it.</p>
<p>So, for example, if you navigate to /kittens/1-my-fluffy-kitten navigation link for &#8216;Kittens&#8217; will have &#8216;active&#8217; class attached to it. It&#8217;s almost like magic.</p>
<p>You probably noticed that &#8216;Home&#8217; is highlighted as well. It happened because whatever URL you are currently on is a child of the home_path. I guess we want to mark it as active only if we find ourselves on the home page and not anywhere else. We can fix this:</p>
<div class="codecolorer-container ruby default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="ruby codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color:#006600; font-weight:bold;">%</span>ul<br />
&nbsp; <span style="color:#006600; font-weight:bold;">%</span>li= active_link_to <span style="color:#996600;">'Home'</span>, home_path, <span style="color:#ff3333; font-weight:bold;">:active</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006600; font-weight:bold;">&#123;</span>:<span style="color:#9966CC; font-weight:bold;">when</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#ff3333; font-weight:bold;">:self_only</span><span style="color:#006600; font-weight:bold;">&#125;</span><br />
&nbsp; <span style="color:#006600; font-weight:bold;">%</span>li= active_link_to <span style="color:#996600;">'Puppies'</span>, puppies_path<br />
&nbsp; <span style="color:#006600; font-weight:bold;">%</span>li= active_link_to <span style="color:#996600;">'Kittens'</span>, kittens_path<br />
&nbsp; <span style="color:#006600; font-weight:bold;">%</span>li= active_link_to <span style="color:#996600;">'Froggies'</span>, froggies_path</div></div>
<p>Hey, wanna render a sub nav when you find yourself browsing /puppies or any page under that URL? It&#8217;s just sooo easy:</p>
<div class="codecolorer-container ruby default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="ruby codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color:#006600; font-weight:bold;">%</span>ul<br />
&nbsp; <span style="color:#006600; font-weight:bold;">%</span>li= active_link_to <span style="color:#996600;">'Home'</span>, home_path, <span style="color:#ff3333; font-weight:bold;">:active</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006600; font-weight:bold;">&#123;</span>:<span style="color:#9966CC; font-weight:bold;">when</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#ff3333; font-weight:bold;">:self_only</span><span style="color:#006600; font-weight:bold;">&#125;</span><br />
&nbsp; <span style="color:#006600; font-weight:bold;">%</span>li<br />
&nbsp; &nbsp; = active_link_to <span style="color:#996600;">'Puppies'</span>, puppies_path<br />
&nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">-</span> <span style="color:#9966CC; font-weight:bold;">if</span> is_active_link?<span style="color:#006600; font-weight:bold;">&#40;</span>puppies_path<span style="color:#006600; font-weight:bold;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">%</span>ul<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">%</span>li= active_link_to <span style="color:#996600;">'Big'</span>, big_puppies_path<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">%</span>li= active_link_to <span style="color:#996600;">'Small'</span>, small_puppies_path<br />
&nbsp; <span style="color:#006600; font-weight:bold;">%</span>li= active_link_to <span style="color:#996600;">'Kittens'</span>, kittens_path<br />
&nbsp; <span style="color:#006600; font-weight:bold;">%</span>li= active_link_to <span style="color:#996600;">'Froggies'</span>, froggies_path</div></div>
<p>And that&#8217;s the skinny of what active_link_to is for.</p>
<p>For more documentation on more functionality checkout project on GitHub: <a href="http://github.com/theworkinggroup/active_link_to">http://github.com/theworkinggroup/active_link_to</a></p>
]]></content:encoded>
			<wfw:commentRss>http://blog.twg.ca/2009/11/active-state-for-link_to-active_link_to/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
