【问题标题】:Rails 3 Apply CSS class if path matches root:to pathRails 3如果路径匹配根应用CSS类:到路径
【发布时间】:2011-06-23 00:39:08
【问题描述】:

我对 ROR 非常陌生,并且在我开发我的第一个应用程序时就喜欢它。当我将格式应用到导航菜单时,我有一个与我的应用程序模板相关的问题。

是否可以检查 url 路径是否与 config.rb 中设置的 root:to 路径匹配?我有一个帮助方法,它返回字符串“current”,它添加了 css 类样式以突出显示选定的菜单项。只要我不在主页上,辅助方法就可以正常工作。当我的 url 是 www.localhost:3000/ 时,css 当前类不适用于 Products 链接,因为 request_uri = "/" 不等于 "/products"。当我在主页上时,我希望将 css 类“当前”应用于产品菜单项。

是否有任何条件逻辑可以用来获取 root:to 路径并检查它是否与 is_current 的参数路径匹配?

这是我的代码:

routes.rb 根目录:设置为指向产品索引视图

root :to => 'products#index'

application.html.erb

  • is_current(products_path) %>
  • is_current(reports_path) %>
  • application_helper.rb

    def is_current(path)
        if request.request_uri == path
            return 'current'
        end
    end
    

    任何帮助将不胜感激。

    谢谢, 卡森

    【问题讨论】:

      标签: ruby-on-rails-3 menu navigation helper selecteditem


      【解决方案1】:

      这对你有用吗?

      if current_page? root_path
      

      更多信息:http://api.rubyonrails.org/classes/ActionView/Helpers/UrlHelper.html#method-i-current_page%3F

      【讨论】:

      • señor poopypants never come back to select the right answer :/
      【解决方案2】:

      如果我没看错,你想应用“onlink”css 吗?这样当用户在首页时,首页图标是不同的颜色什么的。如果是这样,那么将其应用于您的部分标题或任何地方:

      <li><%= link_to_unless_current "Home", root_path, :class => "navLinks" do link_to "Home", root_path, :class => "navLinks", :id => "onlink" end %></li>

      • 我知道这是一个老问题,但它可能对其他人有用:)

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-01-20
        • 2019-05-23
        • 2012-11-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多