添加“当前”的最佳方式在Rails 3中导航的类

我在导航菜单中有一些静态页面。我想在当前显示的项目中添加类似“当前”的类。 我这样做的方法是添加大量的辅助方法(每个项目一个)来检查控制器和操作。
def current_root_class
  'class="current"' if controller_name == "homepage" && action_name == "index" 
end

<ul>
  <li <%= current_root_class %>><%= link_to "Home", root_path %>
有没有更好的方法呢??我目前的方式是如此愚蠢......     
已邀请:
这里不是真正的答案,因为我使用的方式和你一样。我刚刚定义了帮助方法来测试多个控制器或操作: 在application_helper.rb中
  def controller?(*controller)
    controller.include?(params[:controller])
  end

  def action?(*action)
    action.include?(params[:action])
  end
然后你可以在你的视图或其他辅助方法中使用
if controller?("homepage") && action?("index", "show")
......     
我做了一个名叫
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
这将生成HTML之类的
<li class="current"><a href="/">Home</a></li>
    
使用
current_page?
帮助器确定是否应该指定
"current"
类。例如:
<%= 'active' if current_page?(home_about_path) %>
请注意,您也可以传递路径(不仅是选项的散列),例如:
current_page?(root_path)
。     
我在application_helper.rb(Rails 3)中使用这个nav_link(文本,链接)函数来完成工作,并为我滚动我的bootstrap twitter 2.0导航栏链接。
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) %>
    
我这样做的方法是在application_helper中添加一个辅助函数
def current_class?(test_path)
  return 'current' if request.request_uri == test_path
  ''
end
然后在导航中,
<%= link_to 'Home', root_path, :class => current_class?(root_path) %>
这将测试针对当前页面uri的链接路径,并返回当前类或空字符串。 我没有对此进行过彻底的测试,而且我对RoR非常陌生(在PHP使用了十年之后),所以如果这有一个重大缺陷,我很乐意听到它。 至少这样你在每个链接中只需要1个辅助函数和一个简单的调用。     
要建立@Skilldrick的答案...... 如果您将此代码添加到application.js,它将确保具有活动子项的任何下拉菜单也将标记为活动...
$('.active').closest('li.dropdown').addClass('active');
要回顾支持代码>添加一个名为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
用过:
nav_link_to 'Home', root_path
这将生成HTML之类的
<li class="active"><a href="/">Home</a></li>
    
我认为最好的方法是 application_helper.rb:
def is_active(controller, action)       
  params[:action] == action && params[:controller] == controller ? "active" : nil        
end
并在菜单中:
<li class="<%= is_active('controller', 'action') %>">
    
我知道这是一个过时的答案,但你可以通过使用一个名为active_link_to gem的link_to帮助包装器轻松忽略所有这些当前页面检查,它可以正常工作,为当前页面链接添加一个活动类     
以下是有关如何在rails视图中的bootstrap菜单页面上添加活动类的完整示例。
    <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>
    
我使用了一个名为Tabs on Rails的精彩宝石。     
就我个人而言,我在这里使用了一些答案
<li class="<%= 'active' if current_page?(inventory_index_path) %>"><a href="#">Menu</a></li>
我正在使用materialize css和我使主要类别可折叠的方法是使用下面的代码
$('.active').closest(".collapsible.collapsible-accordion")
            .find(".collapsible-header")
            .click();
希望它可以帮到某人     
current_page?
方法对我来说不够灵活(比如说你设置了一个控制器而不是一个动作,那么它只会在控制器的索引动作上返回true),所以我根据其他答案做了这个:
  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'
    
我有一个更简洁的nav_link版本,其工作原理与link_to完全相同,但是可以自定义以输出包装li标签。 将以下内容放在application_helper.rb中
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
如果您查看上面的代码并将其与url_helper.rb中的link_to代码进行比较,唯一的区别是它检查url是否是当前页面,并将类“active”添加到包装li标记。这是因为我在Twitter Bootstrap的nav组件中使用nav_link助手,它更喜欢将链接包含在li标签内,并将“active”类应用于外部li。 上面代码的好处是它允许你将一个块传入函数,就像你可以使用link_to一样。 例如,带图标的引导程序导航列表如下所示: 瘦:
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>
此外,就像link_to帮助器一样,您可以将HTML选项传递到nav_link,该选项将应用于a标签。 传入锚点标题的示例: 瘦:
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>
    
是的!看看这篇文章:在Rails中为链接添加“选定”类的更好方法 将nav_link_helper.rb拖放到app / helpers中,它可以像以下一样简单:
<%= nav_link 'My_Page', 'http://example.com/page' %>
nav_link助手的工作方式与标准的Rails link_to助手类似,但如果满足某些条件,则会向链接(或其包装器)添加“选定”类。默认情况下,如果链接的目标网址与当前网页的网址相同,则会在链接中添加默认的“已选择”类。 这里有一个要点:https://gist.github.com/3279194 更新:这是一个宝石:http://rubygems.org/gems/nav_link_to     
我使用这样的简单助手进行顶级链接,因此
/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
    
让我展示我的解决方案: _header.html.erb:
  <ul class="nav">
    <%= nav_tabs(@tabs) %> 
  </ul>
application_helper.rb:
 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
application_controller.rb:
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
    
根据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
使其更有用。     
此版本基于@ Skilldrick的版本,但允许您添加html内容。 因此,您可以这样做:
nav_link "A Page", a_page_path
但是也:
nav_link a_page_path do
  <strong>A Page</strong>
end
或任何其他html内容(例如,您可以添加图标)。 这里的助手是:
  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
    
我想我想出了一个简单的解决方案,可能对很多用例有帮助。这让我: 不仅支持纯文本,还支持
link_to
内的HTML(例如,在链接中添加一个图标) 添加几行代码到
application_helper.rb
active
附加到link元素的整个类名,而不是它是唯一的类。 所以,将其添加到
application_helper.rb
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和资源。     
所有这些都使用简单的导航栏,但下拉子菜单怎么样? 选择子菜单时,顶部菜单项应为“当前” 在这种情况下,tabs_on_rails我是解决方案     
这就是我在当前项目中解决的问题。
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
    
我的简单方法 -
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
谢谢。     
如果您还想在视图中支持html选项哈希。例如,如果要使用其他CSS类或id调用它,可以像这样定义辅助函数。
def nav_link_to(text, url, options = {})
  options[:class] ||= ""
  options[:class] += " active"
  options[:class].strip!
  link_to text, url, options
end
因此,在视图中,调用此帮助程序的方式与调用link_to帮助程序的方式相同
<%= nav_link_to "About", about_path, class: "my-css-class" %>
    
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
现在在下面的用例中使用它。 1.对于任何特定控制器的所有动作
<li class="<%= active('controller_name')%>">
....
</li>
2.对于许多控制器的所有动作(以逗号分隔)
<li class="<%= active('controller_name1,controller_name2')%>">
....
</li>
3.对于任何特定控制器的特定操作
<li class="<%= active('controller_name', 'action_name')%>">
....
</li>
4.对于许多控制器的特定动作(以逗号分隔)
<li class="<%= active('controller_name1,controller_name2', 'action_name')%>">
....
</li>
5.对于任何特定控制器的某些特定操作
<li class="<%= active('controller_name', 'index, show')%>">
....
</li>
6.对于许多控制器的某些特定操作(以逗号分隔)
<li class="<%= active('controller_name1,controller_name2', 'index, show')%>">
....
</li>
希望能帮助到你     

要回复问题请先登录注册