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

后端 未结 24 1753
天涯浪人
天涯浪人 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:35

    According to the answer by Skilldrick, I'll change it to the following:

    def nav_link(*args, &block)
      is_active = current_page?(args[0]) || current_page?(args[1])
      class_name = is_active ? 'active' : nil
    
      content_tag(:li, class: class_name) do
        link_to *args, &block
      end
    end
    

    to make it much more useful.

提交回复
热议问题