代码之家  ›  专栏  ›  技术社区  ›  PeterWong

将“当前”类添加到Rails 3中导航的最佳方法

  •  110
  • PeterWong  · 技术社区  · 14 年前

    我在导航菜单中有一些静态页面。我想在当前显示的项目中添加一个类似“current”的类。

    我这样做的方法是添加大量助手方法(每个方法用于一个项目)来检查控制器和操作。

    def current_root_class
      'class="current"' if controller_name == "homepage" && action_name == "index" 
    end
    
    <ul>
      <li <%= current_root_class %>><%= link_to "Home", root_path %>
    

    有没有更好的办法呢?我现在的方式太愚蠢了……

    24 回复  |  直到 7 年前
        1
  •  55
  •   Yannis    14 年前

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

    if controller?("homepage") && action?("index", "show")

        2
  •  287
  •   Skilldrick    13 年前

    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
    

    nav_link 'Home', root_path
    

    <li class="current"><a href="/">Home</a></li>
    
        3
  •  68
  •   Juan Pablo Ugas    8 年前

    current_page? "current"

    <%= 'active' if current_page?(home_about_path) %>
    

    current_page?(root_path)

        4
  •  26
  •   Peyton    12 年前

    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
    

    <%=nav_link("About Us", about_path) %>
    
        5
  •  10
  •   fullybaked user2362083    14 年前

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

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

        6
  •  6
  •   Praveen Kumar Purushothaman Daniel Dewhurst    12 年前

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

    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
    

    nav_link_to 'Home', root_path
    

    <li class="active"><a href="/">Home</a></li>
    
        7
  •  4
  •   IPIvliev    10 年前

    def is_active(controller, action)       
      params[:action] == action && params[:controller] == controller ? "active" : nil        
    end
    

    <li class="<%= is_active('controller', 'action') %>">
    
        8
  •  4
  •   fuyi    10 年前
        9
  •  4
  •   Little Phild    9 年前

        <li class="<%= 'active' if current_page?(root_path) %>"><%= link_to 'Home', controller: "welcome" %></li>
        <li class="<%= 'active' if current_page?(about_path) %>"><%= link_to 'About us', about_path %></li>
       <li class="<%= 'active' if current_page?(contact_path) %>"><%= link_to 'Contact us', contact_path %></li>
    
        10
  •  3
  •   icecream    14 年前
        11
  •  3
  •   Petros Kyriakou    8 年前

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

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

        12
  •  2
  •   unrelativity    13 年前

    current_page?

      def nav_link_to(link_text, link_path, checks=nil)
        active = false
        if not checks.nil?
          active = true
          checks.each do |check,v|
            if not v.include? params[check]
              active = false
              break
            end
          end
        end
    
        return content_tag :li, :class => (active ? 'active' : '') do
          link_to link_text, link_path
        end
      end
    

    nav_link_to "Pages", pages_url, :controller => 'pages'
    
        13
  •  2
  •   Joe Chen    12 年前

    def nav_link(*args, &block)
        if block_given?
          options      = args.first || {}
          html_options = args.second
          nav_link(capture(&block), options, html_options)
        else
          name         = args[0]
          options      = args[1] || {}
          html_options = args[2]
    
          html_options = convert_options_to_data_attributes(options, html_options)
          url = url_for(options)
    
          class_name = current_page?(url) ? 'active' : nil
    
          href = html_options['href']
          tag_options = tag_options(html_options)
    
          href_attr = "href=\"#{ERB::Util.html_escape(url)}\"" unless href
          "<li class=\"#{class_name}\"><a #{href_attr}#{tag_options}>#{ERB::Util.html_escape(name || url)}</a></li>".html_safe
        end
      end
    

    nav component

    ul.nav.nav-list
      =nav_link root_path do
        i.icon-home
        |  Home
      =nav_link "#" do
        i.icon-user
        |  Users
    

    <ul class="nav nav-list">
      <li class="active">
        <a href="/">
          <i class="icon-home"/>
          Home
        </a>
      </li>
      <li>
        <a href="#">
          <i class="icon-users"/>
          Users
        </a>
      </li>
    </ul>
    

    ul.nav.nav-list
      =nav_link root_path, title:"Home" do
        i.icon-home
        |  Home
      =nav_link "#", title:"Users" do
        i.icon-user
        |  Users
    

    <ul class="nav nav-list">
      <li class="active">
        <a href="/" title="Home">
          <i class="icon-home"/>
          Home
        </a>
      </li>
      <li>
        <a href="#" title="Users">
          <i class="icon-users"/>
          Users
        </a>
      </li>
    </ul>
    
        15
  •  2
  •   complistic    11 年前

    /stories/my-story /stories

    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
    
        16
  •  2
  •   Mike Andrianov    10 年前

      <ul class="nav">
        <%= nav_tabs(@tabs) %> 
      </ul>
    

     def nav_tabs(tabs=[])
        html = []
        tabs.each do |tab| 
          html << (content_tag :li, :class => ("current-page" if request.fullpath.split(/[\??]/)[0] == tab[:path]) do
            link_to tab[:path] do
              content_tag(:i, '', :class => tab[:icon]) +
              tag(:br) +
              "#{tab[:name]}"
            end
          end)        
        end
    
        html.join.html_safe
      end
    

    before_filter :set_navigation_tabs
    
    private
    def set_navigation_tabs
      @tabs = 
        if current_user && manager?
          [
            { :name => "Home", :icon => "icon-home", :path => home_index_path },
            { :name => "Portfolio", :icon => "icon-camera", :path => portfolio_home_index_path },
            { :name => "Contact", :icon => "icon-envelope-alt", :path => contact_home_index_path }
          ]
        elsif current_user && client?
          ...
        end
    
        17
  •  1
  •   Community Jaime Torres    7 年前

    answer by Skilldrick

    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
    

        18
  •  1
  •   Benjamin J. Benoudis    10 年前

    nav_link "A Page", a_page_path

    nav_link a_page_path do
      <strong>A Page</strong>
    end
    

      def nav_link(name = nil, options = nil, html_options = nil, &block)
        html_options, options, name = options, name, block if block_given?
        options ||= {}
    
        html_options = convert_options_to_data_attributes(options, html_options)
    
        url = url_for(options)
        html_options['href'] ||= url
    
        class_name = current_page?(url) ? 'current' : ''
        content_tag(:li, :class => class_name) do  
          content_tag(:a, name || url, html_options, &block)
        end
      end
    
        19
  •  1
  •   Community Jaime Torres    7 年前

    • link_to
    • application_helper.rb
    • active

    def active_class?(class_name = nil, path)
      class_name ||= ""
      class_name += " active" if current_page?(path)
      class_name.strip!
      return class_name
    end
    

    <div class="col-xs-3">
      <%= link_to root_path, :class => active_class?("btn btn-outline-primary", root_path) do %>
        <i class="fa fa-list-alt fa-fw"></i>
      <% end %>
    </div>
    

    class_name <div class="<%= current_page?(root_path) %>">

    1 2 resources

        20
  •  0
  •   user762579    12 年前

        21
  •  0
  •   GN.    11 年前

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

    li = link_to company_path 
        class: %w{ class_if_current_page( { status: "pending" }, "active" ), "company" } do  
          Current Company
    
        22
  •  0
  •   Lwin Htoo Ko    9 年前

    application.html.erb

    <div class="navbar">
        <div class="<%= @menu1_current %> first-item"><a href="/menu1"> MENU1 </a></div>
        <div class="<%= @menu2_current %>"><a href="/menu2"> MENU2 </a></div>
        <div class="<%= @menu3_current %>"><a href="/menu3"> MENU3 </a></div>
        <div class="<%= @menu4_current %> last-item"><a href="/menu4"> MENU4 </a></div>
    </div>
    

    main_controller.erb

    class MainController < ApplicationController
        def menu1
            @menu1_current = "current"
        end
    
        def menu2
            @menu2_current = "current"
        end
    
        def menu3
            @menu3_current = "current"
        end
    
        def menu4
            @menu4_current = "current"
        end
    end
    

        23
  •  0
  •   Ken Hibino    9 年前

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

    <%= nav_link_to "About", about_path, class: "my-css-class" %>
    
        24
  •  0
  •   Lalit Kumar Maurya    7 年前

    ApplicationHelper

    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
    

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

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

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

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

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

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