Changing Current Tab in Rails

后端 未结 8 1067
北荒
北荒 2020-12-28 10:05

I have a list of tabs at the top of my application that I include in a general layout in application.html.erb. They look like this:

  • 8条回答
    •  天命终不由人
      2020-12-28 10:33

      I'm so not claiming this is the best way to do this, however I am brave enough to post what I came up with :)

      Some example menu links from my layout:

      
      
      

      Then I created this helper:

      def menu_link_to(*args, &block)
        controller = args.shift
        action = args.shift
      
        if controller == controller_name && (action.nil? || action == action_name)
          if args.third.nil?
            args.push({:class => 'selected'})
          else
            args.third.merge!({:class => 'selected'})
          end
        end
      
        link_to *args, &block
      end
      

    提交回复
    热议问题