'link_to_class_current' helper

I'm trying to create a navigation which adds :class => "current" to the currently selected nav link (just like the WordPress nav). I presume the class would be determined by the controller params (which isn't a problem as all my tabs are named after a controller). If so, how I would go about implementing this on the following example;

<ul>   <li><%= link_to 'Users', users_path %></li>   <li><%= link_to 'Posts', posts_path %></li>   <li><%= link_to 'Pages', page_path %></li> </ul>

I made some attempts at Googling this and subsequently came across a snippet on dzone, http://snippets.dzone.com/posts/show/1996 , but the plugin link appears to be dead.

If only there was a 'link_to_class_current' helper!

Neil Cauldwell wrote:

<ul>   <li><%= link_to 'Users', users_path %></li>   <li><%= link_to 'Posts', posts_path %></li>   <li><%= link_to 'Pages', page_path %></li> </ul>

I made some attempts at Googling this and subsequently came across a snippet on dzone, http://snippets.dzone.com/posts/show/1996 , but the plugin link appears to be dead.

If only there was a 'link_to_class_current' helper!

by class => "current", I assuming that you want to add a css class attribute to link_to which happens to be specified within the third argument in the method

link_to 'Users', users_path, :class => "current"

If on the other hand, you wanted to pass this class => "current" to your controller as params then you can do it within the second argument of link_to:

link_to 'Users', {:action => 'index', :class => "current"}

please check http://api.rubyonrails.org/

hth

ilan

Ilan Berci wrote:

Neil Cauldwell wrote:

<ul>   <li><%= link_to 'Users', users_path %></li>   <li><%= link_to 'Posts', posts_path %></li>   <li><%= link_to 'Pages', page_path %></li> </ul>

I made some attempts at Googling this and subsequently came across a snippet on dzone, http://snippets.dzone.com/posts/show/1996 , but the plugin link appears to be dead.

If only there was a 'link_to_class_current' helper!

by class => "current", I assuming that you want to add a css class attribute to link_to which happens to be specified within the third argument in the method

link_to 'Users', users_path, :class => "current"

If on the other hand, you wanted to pass this class => "current" to your controller as params then you can do it within the second argument of link_to:

link_to 'Users', {:action => 'index', :class => "current"}

please check http://api.rubyonrails.org/

hth

Ilan, I wanted to dynamically generate a "current" css class in nav links based on the name of the current controller. I'm happy with adding css class options to link_to - I just don't know how to do this with conditional statements. For example, take the following URLs;

/users/ /users/1 /users/1/posts (I appreciate this one may complicate things)

For all those URLs, I would expect my nav to generate the following navigation (because the UsersController, or UsersPostsController, is in use);

<ul>   <li><%= link_to 'Users', users_path, :class => "current" %></li>   <li><%= link_to 'Posts', posts_path %></li>   <li><%= link_to 'Pages', pages_path %></li> </ul>

I have RailsBrain in my bookmarks but I haven't found anything that specifically relates to my issue; the link_to family of methods don't see to cover this. Do you have any further suggestions?

Neil Cauldwell wrote:

I have RailsBrain in my bookmarks but I haven't found anything that specifically relates to my issue; the link_to family of methods don't see to cover this. Do you have any further suggestions?

Neil,

Ohhh.. ok, now I think I understand you, if you want to get the current controller in the view, just pass it in..

In your controller: (before sending it off to the view) @class = self.class.name

In your view, <%= link_to 'Users', :action => "whatever", :class => @class %>

hth

ilan

link_to_unless_current supports this behavior quite nicely.

By default, it won’t make a link at all for the current action which can be quite nice, but the method supports a block that acts as the “unless” condition as well.

<%=link_to_unless_current(“Users”, users_path) do link_to("Users, :users_path, {:class=>:current}) %>

Or

<%=link_to_unless_current(“Users”, users_path) do

Users’ %>

Or however you see fit.