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;
- <%= link_to 'Users', users_path %>
- <%= link_to 'Posts', posts_path %>
- <%= link_to 'Pages', page_path %>
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 C. wrote:
- <%= link_to 'Users', users_path %>
- <%= link_to 'Posts', posts_path %>
- <%= link_to 'Pages', page_path %>
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 B. wrote:
Neil C. wrote:
- <%= link_to 'Users', users_path %>
- <%= link_to 'Posts', posts_path %>
- <%= link_to 'Pages', page_path %>
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);
- <%= link_to 'Users', users_path, :class => "current" %>
- <%= link_to 'Posts', posts_path %>
- <%= link_to 'Pages', pages_path %>
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 C. 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
Ilan B. wrote:
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
ilan, that puts the controller name as a class in the link, so it
differentiates the link and will work - but I’d rather the link was
explicity given the class “current”, because otherwise I’ll need to
include “UserController, PostController” and every other type of
controller name, within the stylesheet declaration for the ‘currently
selected tab’.
Maybe it would help if you looked at my WordPress site:
http://dotneil.com. You’ll see that the parent tab is given a
“current_page_item”, so when you hit the homepage, the ‘Blog’ tab is
highlighted.
I can achieve the same effect in a dirty, non-DRY way using condition
statements against every single tab, i.e. <% if params[:controller] ==
“users” %>, but, alongside being messy, this creates ambiguities with
nested resources.
Neil C. wrote:
I can achieve the same effect in a dirty, non-DRY way using condition
statements against every single tab, i.e. <% if params[:controller] ==
“users” %>, but, alongside being messy, this creates ambiguities with
nested resources.
something like this then?
#assuming that the controller was passed in as discussed before
html_options = {}
html_options.merge(:class => ‘current’) if @class ==
UserController.class.name
<%= link_to ‘Users’, users_path, html_options %>
also, check out current_page? in the rails api…
I know you have been explaining your heart out on this so I truly hope
this gives you some clues… thanks for being patient…
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.
On Fri, Mar 7, 2008 at 9:34 PM, Ilan B.
[email protected]
Ilan B. wrote:
I know you have been explaining your heart out on this so I truly hope
this gives you some clues… thanks for being patient…
ilan
No need to apologise - you’ve been a great help - and not for the first
time, either!
‘current_page?’ may do the trick. I just realised the nav will use funky
CSS dropdowns to contain more than one link per list item, which
completely throws a spanner in things;
- <%= link_to 'Users', users_path %>
- <%= link_to 'Posts', posts_path %>
- <%= link_to 'Pages', pages_path %>
I just tried a quick ‘n’ dirty route;
<%= link_to
'Users', users_path %>
And I think I could get that working quite nicely if I can work out how
to return “current” if true, nil if false, and also include multiple
controller actions, too. Specifying the :controller without an :action
only returns true if the current_page is the ‘index’, which baffles me.
This doesn’t work, but if I could do something like it, I’d be all set;
"><%= link_to 'Users', users_path %>
Brian H. wrote:
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})
%>
Thanks Brian, the link_to_unless_current trick absolutely hits the
previous requirements on the head. It’s just a shame that I may need to
add the class=“current” to the parent element instead. Argh!