Mats Persson
2005-Sep-20 08:52 UTC
Conditional Menu in View = How do you create handle this scenario ?
Hi all, <NewbieAlert> I''m just working on this site with a main_nav menu that should highlight the active section on each request. This code worked perfectly when I had all the sections as methods in [ /app/ controllers/application.rb ], but not when I began transforming some sections [ skills, clients, portfolio ] to their own controllers. I guess I should use something like [ if @params[:controller] =~ / ^<controller_name>/ ] to test for each of the controllers, but that seems to unsophisticated for Ruby/Rails, so I''m hoping there''s a smarter way. Is there ? def main_nav # temporary array of possible menu items menus = %w{ home experience skills clients portfolio contact } nav = String.new nav << "<ul class=\"leftNav\">\n" menus.each do |menu| if controller.action_name.downcase == menu.downcase nav << "<li><a href=\"#\" class=\"active\">#{menu.capitalize} </a></li>\n" else if menu.downcase == ''home'' nav << "<li><a href=\"/\">#{menu.capitalize}</a></li>\n" else nav << "<li><a href=\"/#{menu.downcase}/\"># {menu.capitalize}</a></li>\n" end end end nav << "</ul>\n" end </NewbieAlert> Kind regards, Mats ---- "TextMate, coding with an incredible sense of joy and ease" - www.macromates.com -
Jason Gilstrap
2005-Sep-20 12:44 UTC
Re: Conditional Menu in View = How do you create handle this scenario ?
Have you considered using either link_to_unless or link_to_unless_current? http://api.rubyonrails.com/classes/ActionView/Helpers/UrlHelper.html#M000310 -Jason Gilstrap info-Yf9cRGTgqmECWBydzj52wkEOCMrvLtNR@public.gmane.org Mats Persson wrote:> > Hi all, > > <NewbieAlert> > > I''m just working on this site with a main_nav menu that should > highlight the active section on each request. This code worked > perfectly when I had all the sections as methods in [ /app/ > controllers/application.rb ], but not when I began transforming some > sections [ skills, clients, portfolio ] to their own controllers. > > I guess I should use something like [ if @params[:controller] =~ / > ^<controller_name>/ ] to test for each of the controllers, but that > seems to unsophisticated for Ruby/Rails, so I''m hoping there''s a > smarter way. Is there ? > > > def main_nav > # temporary array of possible menu items > menus = %w{ home experience skills clients portfolio contact } > nav = String.new > nav << "<ul class=\"leftNav\">\n" > menus.each do |menu| > if controller.action_name.downcase == menu.downcase > nav << "<li><a href=\"#\" class=\"active\">#{menu.capitalize} > </a></li>\n" > else > if menu.downcase == ''home'' > nav << "<li><a href=\"/\">#{menu.capitalize}</a></li>\n" > else > nav << "<li><a href=\"/#{menu.downcase}/\"># > {menu.capitalize}</a></li>\n" > end > end > end > nav << "</ul>\n" > end > > </NewbieAlert> > > > Kind regards, > > Mats > > ---- > "TextMate, coding with an incredible sense of joy and ease" > - www.macromates.com - > > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails