【问题标题】:uninitialized constant Tilt::CompileSite::Nokogiri error in sinatra/hamlsinatra/haml 中未初始化的常量 Tilt::CompileSite::Nokogiri 错误
【发布时间】:2010-10-21 23:30:44
【问题描述】:

我有一个在 ruby​​ 中运行良好的两行脚本。我把它移到haml并得到错误

NameError at / uninitialized constant Tilt::CompileSite::Nokogiri

haml 代码:

      %td
        - @doc = Nokogiri::XML(File.open(file))
        = @doc.xpath("//testsuite").each_with_index {|node,index| "#{index+1}. #{node.attributes["name"].value}<BR>" }

知道如何让它发挥作用吗?

在 Win XP SP3 上使用下面

  • ruby 1.8.7(2010-08-16 补丁级别 302)[i386-mingw32]
  • nokogiri (1.4.3.1 x86-mingw32)
  • 西纳特拉 (1.0)
  • 薄 (1.2.7 x86-mswin32)

【问题讨论】:

    标签: ruby sinatra nokogiri


    【解决方案1】:

    试试这个:

    %td
      - @doc = ::Nokogiri::XML(File.open(file))
      = @doc.xpath("//testsuite").each_with_index {|node,index| "#{index+1}. #{node.attributes["name"].value}<BR>" }
    

    【讨论】:

    • 而不是1. login 2. AddCycleTemplate 3. AddAcademicYear 在三行我得到0 name description 0 name description 0 name description 在一行。所以节点数是正确的,但索引始终为 0,name 也不正确。但我没有得到最初的错误。这是伟大的。谢谢。
    • 好吧,没想到。你不想渲染 each_with_index 的返回值:gist.github.com/e21140baeda491152510
    【解决方案2】:

    仍然不知道为什么会发生错误,但我通过在 sinatra 文件中定义一个函数并在 haml 文件 = get_testsuite(file) 中调用它来解决它

    def get_testsuite (file)
    
      @doc = Nokogiri::XML(File.open(file))
      output = Array.new
      @doc.xpath("//testsuite").each_with_index {|node,index|
        output << "#{index+1}. #{node.attributes["name"].value}<BR>"
      }
      return output
    end
    

    【讨论】:

      猜你喜欢
      • 2014-01-06
      • 2016-03-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多