【问题标题】:how to highlight current link in rails 3如何突出显示rails 3中的当前链接
【发布时间】:2014-05-08 12:51:17
【问题描述】:

我正在尝试突出显示当前链接。我将此问题用于想法here
我有一个简单的菜单,就是基于这个结构:

<li>
   <%= @waste_root.name %>
   <ul>
      <% @wast_child_ids.each do |it| %>
      <li><%= link_to it.name, products_path(:category =>  it.name), class: "#{cp(products_path)} additional_class" %>   
      </li>
      <%end%>
   </ul>
</li>

在应用助手中:

def cp(path)
  "current" if current_page?(path)
end

在 CSS 文件中:

.current {
color:red;

}

我得到的是所有链接都是红色的。我不明白。对于其他人来说,它工作得很好。

【问题讨论】:

    标签: html css ruby-on-rails


    【解决方案1】:

    我认为你的视野应该是这样的:

    <li>
       <%= @waste_root.name %>
       <ul>
          <% @wast_child_ids.each do |it| %>
          <li><%= link_to it.name, products_path(:category =>  it.name), class: "#{cp(products_path(:category =>  it.name))} additional_class" %>   
          </li>
          <%end%>
       </ul>
    </li>
    

    因为,您在products_path 中传递了参数category,因此current_page? 无法判断正确的相对路径。另外,最好使用_url(complete path) 而不是相对路径。交叉检查和理解也会很清楚。

    【讨论】:

      【解决方案2】:

      您需要传递类别。

      <%= link_to it.name, products_path(:category =>  it.name), class: "#{cp(products_path(:category =>  it.name))} additional_class" %> 
      

      它们都在产品路径上,因为您只是根据参数进行区分。因此,只需传递它们都返回 true 的产品路径,您需要通过传递参数来区分参数,就像您在链接中所做的那样

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2015-04-02
        • 1970-01-01
        • 2011-11-24
        • 2016-09-25
        • 2010-09-15
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多