Css 添加一个';主动';类中的所有活动链接?

Css 添加一个';主动';类中的所有活动链接?,css,ruby-on-rails,views,link-to,view-helpers,Css,Ruby On Rails,Views,Link To,View Helpers,基本上,我有很多这样的代码: link_to t('.profile'), business_path(@business), class: '#{'active' if current_page? business_path(@business)}' 这不是很干 我想知道是否有人知道一种很好的方法来修改link_to helper本身,从而自动将“active”类添加到当前页面的所有链接中 如果有帮助,我愿意使用HAML或SLIM。这是一个很好的例子,可以编写自己的帮助程序来包装链接。在你的

基本上,我有很多这样的代码:

link_to t('.profile'), business_path(@business), class: '#{'active' if current_page? business_path(@business)}'
这不是很干

我想知道是否有人知道一种很好的方法来修改link_to helper本身,从而自动将“active”类添加到当前页面的所有链接中


如果有帮助,我愿意使用HAML或SLIM。

这是一个很好的例子,可以编写自己的帮助程序来包装链接。在你的应用程序\u helper.rb中,你可以编写一个方法
active\u link\u to
,该方法采用与link\u to+current\u页面相同的参数,然后像上面那样调用link\u。

这是我使用的帮助程序。我添加了一个可选的“match_text”参数以增加灵活性(例如,如果我想在实际请求路径是链接目的地的子页面时将链接标记为活动)

def link_to_active(文本、目标、选项={})
匹配文本=选项。删除(:匹配文本)
类别=选项[:类别]。是否存在?选项[:类]。拆分(“”:[]
classes classes.join(“”),除非classes.empty?
链接到(文本、目的地、选项)
结束

使用,然后让它看起来像CSS中的活动链接。

这是一个已解决的问题,只需使用gem即可。您的示例简化为:

= active_link_to t('.profile'), business_path(@business)

当您可以在
html\u选项中指定自定义
class
名称时,我使用内置视图helper
current\u page?
编写了简单的helper方法

def active_link_to(name = nil, options = nil, html_options = nil, &block)
  active_class = html_options[:active] || "active"
  html_options.delete(:active)
  html_options[:class] = "#{html_options[:class]} #{active_class}" if current_page?(options)
  link_to(name, options, html_options, &block)
end
示例(当您在
根路径
路由上时):


# 
# 
# 

我面临同样的要求,下面是我的解决方案

ApplicationHelper

def active_class(link_path)
    current_page?(link_path) ? "active" : ""
end
在你看来:

    <li class="<%= active_class('/') %>">
      <%= link_to 'HOME', root_path %>
    </li>

  • 我和@egyamado做了同样的事。我也需要使用令人敬畏的图标,因此:

    助手:

    def active_class?(link_path)
        'active' if current_page?(link_path)
    end
    
    我的观点是:

     <%= link_to my_controller_page_path,
        :title => "My Controller Page",
        :class => "other_name_class #{active_class?(my_controller_page_path)}" do  %>
                    <i class="fa fa-fighter-jet"></i>&nbsp;My Controller Page
    <%end%>
    
    “我的控制器页面”,
    :class=>“其他名称类”{active类?(我的控制器页面路径)}”do%>
    我的控制器页面
    
    在另一种链接中,例如在一个Li中

    #In this case I put a extra validation in root_path
    <li class="nav-class <%=active_class?(my_controller_page_path)%> <%='active' if current_page?(root_path) %>">
      <%= link_to my_controller_page_path,
          :title => "Page 1",
          :class => "other_name_class" do  %>
          Page 1
      <%end%>
    </li>
    <li class="nav-class <%=active_class?(my_controller_page_2_path)%>">
      <%= link_to my_controller_page_2_path,
          :title => "Page 2",
          :class => "other_name_class" do  %>
          Page 2
      <%end%>
    </li> 
    
    #在本例中,我在根路径中添加了一个额外的验证
    
  • “第1页”, :class=>“其他名称”do%> 第1页
  • “第2页”, :class=>“其他名称”do%> 第2页

  • 它对我很有用。

    根据rails 6.1,现在我们有了html类名的帮助器

    助手示例

    class_names("foo", "bar")
     # => "foo bar"
    class_names({ foo: true, bar: false })
     # => "foo"
    class_names(nil, false, 123, "", "foo", { bar: true })
     # => "123 foo bar"
    
    你可以这样使用它

    <%= link_to 'Home', root_path, class: class_names('nav-link', { active: current_page?(root_path) }) %>
    
    <a class="nav-link active" href="/">Home</a>
    
    
    
    它将生成这样的html

    <%= link_to 'Home', root_path, class: class_names('nav-link', { active: current_page?(root_path) }) %>
    
    <a class="nav-link active" href="/">Home</a>
    
    
    

    文档是

    这是我处理此问题的自定义方法

      def active_link_to(name = nil, options = nil, html_options = nil, &block)
        if current_page?(options)
          active_class = html_options[:active_class] ? html_options[:active_class] : 'has-text-danger'
          html_options[:class] << "#{html_options[:class]} #{active_class}"
        end
    
        link_to(name, options, html_options, &block)
      end
    

    主动链接非常棒。它允许您包装链接(例如使用
    li
    )并指定其他匹配条件,如使用正则表达式。谢谢由于rails的
    link\u to
    工作方式,当您实际将一个块传递给您的
    active\u link\u to
    时,这会中断。这是最干净、最简单、最简单的解决方案。在我看来,没有必要为应用程序助手插入gem。你能添加一个例子吗?在rails 6.1中,我们有助手来做这件事。我的回答是基于这样的原因:抱歉,我的评论只是想让每个人都看到它
    <%= active_link_to "Menu", root_path, class: 'has-text-dark', active_class: 'has-text-danger' %>