我是 Middleman 的新手,在我的一生中,我似乎无法为我在子目录及其子目录上的导航动态设置事件类。

我有以下帮助程序集,它适用于子目录(例如:/about/),但它不适用于子目录(例如:/about/otherpage.html)

module CustomHelpers
  def nav_link_to(link, url, opts={})
    if request.path == link
      prefix = '<li class="active">'
    else
      prefix = '<li>'
    end
    prefix + link_to(link, url, opts) + "</li>"
  end
end

任何帮助,将不胜感激!

最佳答案

问题解决了!

在浏览中间人博客后,我从 EmberJS 站点中发现了一个天才解决方案,该站点在 GitHub 上公开发布。

澄清一下,他们的 helper 是这样的:

def link_to_page name, url
    path = request.path
    current = path =~ Regexp.new('^' + url[1..-1] + '.*\.html')

    if path == 'index.html' and name == 'about'
      current = true
    end

    class_name = current ? ' class="active"' : ''

    "<li#{class_name}><a href=\"#{url}\">#{name}</a></li>"
  end

然后在 ul 标签中设置你的模板:
<%= link_to_page 'Projects', '/projects' %>

这也适用于子目录及其子目录。

希望有帮助!

关于ruby-on-rails - 中间人 - 子目录和子目录上的事件类,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/25879931/

10-13 06:40