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! -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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 -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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? -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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 -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Ilan Berci 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 > > ilanilan, 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. -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Neil Cauldwell 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 -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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 ''<span class="disabled">Users</span>'' %> Or however you see fit. On Fri, Mar 7, 2008 at 9:34 PM, Ilan Berci <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > Neil Cauldwell 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 > -- > Posted via http://www.ruby-forum.com/. > > > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Ilan Berci 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.. :) > > ilanNo 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; <ul> <li class="current"><%= link_to ''Users'', users_path %></li> <li><%= link_to ''Posts'', posts_path %></li> <li><%= link_to ''Pages'', pages_path %></li> </ul> I just tried a quick ''n'' dirty route; <li class="<%= current_page?(:controller => :users) %>"><%= link_to ''Users'', users_path %></li> 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; <li class="<%= current_page?(:controller => :users, :action => {''show'', ''edit''}) do "current" %>"><%= link_to ''Users'', users_path %></li> Brian Hogan 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! -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---