Highlight tab in menu

前端 未结 5 1986
甜味超标
甜味超标 2020-12-28 18:51

I have a menu which is a ul

Home | Calendar | Settings

I want to highlight (via a css class) the selected tab in the menu.

Some lin

5条回答
  •  渐次进展
    2020-12-28 19:43

    I have the same problem and end up creating helper. basicly it replace the link_to url helper so it wrap the link with li and add class "menu_selected" if the current controller matches the link controller

    usage sample

    <%= menu_link_to "Home", home_path %>
    
    
    def menu_link_to(*args, &block)
      url = args[1]
      mapping =  ActionController::Routing::Routes.recognize_path(url)   
    
      class_property = "menu_selected" if mapping[:controller] == controller.controller_path
      content_tag :li, :class => class_property do
        link_to *args, &block
      end
    end
    
    

提交回复
热议问题