Best way to add “current” class to nav in Rails 3

后端 未结 24 1763
天涯浪人
天涯浪人 2020-11-29 15:05

I have some static pages in a navigation menu. I want to add a class like \"current\" to the item which is currently displaying.

The way I am doing so is to add tons

24条回答
  •  粉色の甜心
    2020-11-29 15:43

    This is how I solved in my current project.

    def class_if_current_page(current_page = {}, *my_class)
        if current_page?(current_page)
          my_class.each do |klass|
            "#{klass} "
          end
        end
      end
    

    Then..

    li = link_to company_path 
        class: %w{ class_if_current_page( { status: "pending" }, "active" ), "company" } do  
          Current Company
    

提交回复
热议问题