Changing Current Tab in Rails

后端 未结 8 1082
北荒
北荒 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:18

      I made a helper for this that can accept any number of arguments which is useful for nested resources, but it also accepts a single controller name just like the other answers.

      application-helper.rb:

       def is_active(*links)  
         links.each { |link| return "active" if params[:controller] == link }
       end
      

      application.html.erb:

    • ...
    • Or

      Example usage with HAML & nested resource(will be active for any of the provided controller names):

      applcation.html.haml:

      %li{:class => is_active('blogs', 'comments')}
      

    提交回复
    热议问题