Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/59.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails Rail3-显示菜单的智能方式&;是否添加活动样式类?_Ruby On Rails_Ruby On Rails 3 - Fatal编程技术网

Ruby on rails Rail3-显示菜单的智能方式&;是否添加活动样式类?

Ruby on rails Rail3-显示菜单的智能方式&;是否添加活动样式类?,ruby-on-rails,ruby-on-rails-3,Ruby On Rails,Ruby On Rails 3,给定一个Rails 3应用程序,其菜单如下: <ul> <li>Home</li> <li>Books</li> <li>Pages</li> </ul> 家 书 页数 在Rails中,让应用程序知道面包屑、、或何时使某个LIs显示为以下内容的智能方式是什么: <li class="active">Books</li> 书籍 thx我不确定我是否会为您提供一

给定一个Rails 3应用程序,其菜单如下:

<ul>
 <li>Home</li>
 <li>Books</li>
 <li>Pages</li>
</ul>
  • 页数
在Rails中,让应用程序知道面包屑、、或何时使某个LIs显示为以下内容的智能方式是什么:

 <li class="active">Books</li>
  • 书籍

  • thx

    我不确定我是否会为您提供一种聪明的方法,但总比没有好

    如果您的菜单有一些链接-这不在您的示例中,但我认为真正的菜单应该有链接,而不仅仅是项目。例如,HAML中的类似内容:(我使用HAML作为在文本区域中编写ERB的工具,这纯粹是地狱)

    那么这个助手(从我的项目粘贴)应该很方便:

    #
    # Shows link with style "current" in case when the target controller is same as 
    # current
    # beware: this helper has some limitation - it only accepts hash as URL parameter
    #
    def menu_link_to(title, url, html_options = {})
      unless url.is_a?(Hash) and url[:controller]
        raise "URL parameter has to be Hash and :controller has to be specified"
      end
    
      if url[:controller] == controller.controller_path
        html_options[:class] = "current"
      end
    
      link_to(title, url, html_options)
    end
    

    有了这个助手,您可以将上面代码中的“link\u to”替换为“menu\u link\u to”,就这样

    Radek Paviensky的helper的一个修改版本稍微简单一点,更类似于link_to

      # Shows link with style "current" in case when the target controller is same as 
      # current.
      def menu_link_to(title, options = {}, html_options = {})
        if current_page?(options)
          html_options[:class] ||= []
          html_options[:class] << "active" # @TODO catch cases where the class is passed as string instead of array.
        end
    
        link_to(title, options, html_options)
      end
    
    #如果目标控制器与当前控制器相同,则显示样式为“current”的链接
    #现在。
    定义菜单链接(标题,选项={},html选项={})
    如果是当前页面?(选项)
    html|U选项[:类]| |=[]
    html_选项[:类]
    
      # Shows link with style "current" in case when the target controller is same as 
      # current.
      def menu_link_to(title, options = {}, html_options = {})
        if current_page?(options)
          html_options[:class] ||= []
          html_options[:class] << "active" # @TODO catch cases where the class is passed as string instead of array.
        end
    
        link_to(title, options, html_options)
      end