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

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

    I use this nav_link(text, link) function in application_helper.rb (Rails 3) to get the job done and it rolls my bootstrap twitter 2.0 nav bar links for me.

    def nav_link(text, link)
        recognized = Rails.application.routes.recognize_path(link)
        if recognized[:controller] == params[:controller] && recognized[:action] == params[:action]
            content_tag(:li, :class => "active") do
                link_to( text, link)
            end
        else
            content_tag(:li) do
                link_to( text, link)
            end
        end
    end
    

    Example:

    <%=nav_link("About Us", about_path) %>
    
    0 讨论(0)
  • 2020-11-29 15:43

    For me personally i used a combination of answers here

    <li class="<%= 'active' if current_page?(inventory_index_path) %>"><a href="#">Menu</a></li>
    

    I am using materialize css and my way of making the main categories collapsible is by using the code below

    $('.active').closest(".collapsible.collapsible-accordion")
                .find(".collapsible-header")
                .click();
    

    hope it helps someone

    0 讨论(0)
  • 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
    
    0 讨论(0)
  • 2020-11-29 15:43

    If also you want to support html options hash in the view. For example if you want to call it with other CSS class or id, you can define the helper function like this.

    def nav_link_to(text, url, options = {})
      options[:class] ||= ""
      options[:class] += " active"
      options[:class].strip!
      link_to text, url, options
    end
    

    So in the view, call this helper the same way you'd call link_to helper

    <%= nav_link_to "About", about_path, class: "my-css-class" %>
    
    0 讨论(0)
  • 2020-11-29 15:46

    The way I've done it is to add a helper function in the application_helper

    def current_class?(test_path)
      return 'current' if request.request_uri == test_path
      ''
    end
    

    Then in the nav,

    <%= link_to 'Home', root_path, :class => current_class?(root_path) %>
    

    This tests the link path against the current page uri and returns either your current class or an empty string.

    I've not tested this thoroughly and I'm very new to RoR (moving over after a decade with PHP) so if this has a major flaw I'd love to hear it.

    At least this way you only need 1 helper function and a simple call in each link.

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

    all these work with simple nav bars, but what about drop down sub-menu ? when a sub-menu is selected the top menu item should be made 'current' in this case tabs_on_rails me be the solution

    0 讨论(0)
提交回复
热议问题