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

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

    Not truly an answer here, because I'm using quite the same way as you are. I've just defined helper methods to test for multiple controller or actions:

    In application_helper.rb

      def controller?(*controller)
        controller.include?(params[:controller])
      end
    
      def action?(*action)
        action.include?(params[:action])
      end
    

    Then you can use if controller?("homepage") && action?("index", "show") in your views or other helper methods…

    0 讨论(0)
  • 2020-11-29 15:27

    Create a method in ApplicationHelper as below.

    def active controllers, action_names = nil
      class_name = controllers.split(",").any? { |c| controller.controller_name == c.strip } ? "active" : ""
      if class_name.present? && action_names.present?
        return action_names.split(",").any? { |an| controller.action_name == an.strip } ? "active" : ""
      end
      class_name
    end
    

    Now use it in view as below use cases.

    1. For all action of any specific controller

    <li class="<%= active('controller_name')%>">
    ....
    </li>
    

    2. For all action of many controllers (with comma seperated)

    <li class="<%= active('controller_name1,controller_name2')%>">
    ....
    </li>
    

    3. For specific action of any specific controller

    <li class="<%= active('controller_name', 'action_name')%>">
    ....
    </li>
    

    4. For specific action of many controllers (with comma seperated)

    <li class="<%= active('controller_name1,controller_name2', 'action_name')%>">
    ....
    </li>
    

    5. For some specific actions of any specific controller

    <li class="<%= active('controller_name', 'index, show')%>">
    ....
    </li>
    

    6. For some specific actions of many controllers (with comma seperated)

    <li class="<%= active('controller_name1,controller_name2', 'index, show')%>">
    ....
    </li>
    

    Hope it helps

    0 讨论(0)
  • 2020-11-29 15:30

    To build off @Skilldrick 's answer...

    If you add this code to application.js it will make sure that any dropdown menus with active children will also be marked as active...

    $('.active').closest('li.dropdown').addClass('active');
    

    To recap supportive code > Add a helper called nav_link:

    def nav_link_to(link_text, link_path)
      class_name = current_page?(link_path) ? 'active' : ''
    
      content_tag(:li, :class => class_name) do
        link_to link_text, link_path
      end
    end
    

    used like:

    nav_link_to 'Home', root_path
    

    which will produce HTML like

    <li class="active"><a href="/">Home</a></li>
    
    0 讨论(0)
  • 2020-11-29 15:30

    I use an awesome gem called Tabs on Rails.

    0 讨论(0)
  • 2020-11-29 15:30

    I use a simple helper like this for top level links so the /stories/my-story page highlights the /stories link

    def nav_link text, url
    
      active = (url == request.fullpath || (url != '/' && request.fullpath[0..(url.size-1)] == url))
    
      "<li#{ active ? " class='selected'" : '' }><a href='#{url}'>#{text}</a></li>".html_safe
    
    end
    
    0 讨论(0)
  • 2020-11-29 15:31

    I made a helper called nav_link:

    def nav_link(link_text, link_path)
      class_name = current_page?(link_path) ? 'current' : ''
    
      content_tag(:li, :class => class_name) do
        link_to link_text, link_path
      end
    end
    

    used like:

    nav_link 'Home', root_path
    

    which will produce HTML like

    <li class="current"><a href="/">Home</a></li>
    
    0 讨论(0)
提交回复
热议问题