Rails 3如果路径匹配root:to path,则应用CSS类

1 navigation menu selecteditem helper ruby-on-rails-3

我是ROR的新手,我喜欢它,因为我开发了我的第一个应用程序.当我将格式应用于导航菜单时,我有一个与我的应用程序模板相关的问题.

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

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

这是我的代码:

routes.rb root:设置为指向产品索引视图

root:to =>'products #index'

application.html.erb

  • <%= link_to'Products',products_path,:class => is_current(products_path)%>
  • <%= link_to'Reports',reports_path,:class => is_current(reports_path)%>
  • application_helper.rb

    def is_current(path)
        if request.request_uri == path
            return 'current'
        end
    end
    
    Run Code Online (Sandbox Code Playgroud)

    任何帮助将不胜感激.

    谢谢,bkasen