Conditional Menu in View = How do you create handle this scenario ?

Mats Persson
Tue, 20 Sep 2005 08:52:20 -0000

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 << " \n"
     menus.each do |menu|
       if controller.action_name.downcase == menu.downcase
nav << " <a href=\"#\"
class=\"active\">#{menu.capitalize}
\n"
       else
         if menu.downcase == ''home''
nav << " <a
href=\"/\">#{menu.capitalize} \n"
         else
nav << " <a
href=\"/#{menu.downcase}/\">#
{menu.capitalize} \n"
         end
       end
     end
nav << " \n"
   end

</NewbieAlert>


Kind regards,

Mats

----
"TextMate, coding with an incredible sense of joy and ease"
- www.macromates.com -