There are quite a lot of things right about Ruby on Rails. One thing that isn’t is its growth rate, which means that books and tutorials are frequently behind the feature set that is currently stable. This leads to deprecated code or tutorials which no longer match best practice. Correspondingly, when you write Rails code, you’re never sure whether what you’re writing is optimal.
Consider link_to_unless_current, which provides a link to a page, unless you’re already on that specific page. Sounds great, and indeed it is. But it doesn’t do quite what I want. What I want is to have an easy way of highlighting the particular controller in use, and providing links to other controllers, just like the image in this post.
Instead, there’s a more generic method, link_to_unless which provides links based on a condition. You can then write code like this:
<div class="choices">
<ul>
<li><%= link_to_unless ((controller.controller_name == "schedule")), "Schedule", :controller => "schedule" %></li>
<li><%= link_to_unless ((controller.controller_name == "context")), "Contexts", :controller => "context" %></li>
<li><%= link_to_unless ((controller.controller_name == "project")), "Projects", :controller => "project" %></li>
<li><%= link_to_unless ((controller.controller_name == "task")), "Tasks", :controller => "task" %></li>
</ul>
</div>
Of course, you can iterate around a hash if you want to.