机架错误 -- LoadError: 无法加载此类文件 [英] Rack Error -- LoadError: cannot load such file

查看:62
本文介绍了机架错误 -- LoadError: 无法加载此类文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试阅读 tekpub 机架教程,但遇到此错误.

启动错误加载 app.ru 时出现问题加载错误:无法加载此类文件 -- 俳句

在与我尝试运行的应用程序相同的目录中有一个名为 haiku.rb 的文件,但在尝试运行该程序时出现上述错误.代码如下:

class EnvironmentOutput定义初始化(应用程序=零)@app = 应用结尾定义调用(环境)出 = ""除非(@app.nil?)响应 = @app.call(env)[2]输出+=响应结尾env.keys.each {|key|out+="
  • #{key}=#{env[key]}
  • "}[200",{内容类型"=>"text/html"},[out]]结尾结尾需要'haml'需要俳句"类 MyApp定义调用(环境)诗 = Haiku.new.randomtemplate = File.open("views/index.haml").read引擎 = Haml::Engine.new(模板)out = engine.render(Object.new, :poem =>诗)[200",{内容类型"=>文本/html"},输出]结尾结尾使用环境输出运行 MyApp.new

    我确定这是一个小错误,因为代码与教程中的代码相同并且对他有用...

    谢谢

    解决方案

    您需要阅读 ruby​​ 加载路径($LOAD_PATH$:).默认情况下,ruby 有一个加载路径,其中包括您的 gem 安装的位置,这就是为什么您可以执行 require 'haml' 而无需提供您的 haml gem 所在位置的完整路径.

    当您键入 require 'haiku' 时,您基本上是在告诉 ruby​​ 在它的加载路径中的某处查找名为 haiku.rb 的文件,并且 LoadError 来自 ruby​​ 未在 $LOAD_PATH(或 $:)中列出的任何目录中找到您的 haiku.rb 文件,这只是 $LOAD_PATH 的简写.

    您可以通过(至少)两种方式之一解决此问题:

    1. require 'haiku' 改为 require File.dirname(__FILE__) + '/haiku.rb' 以明确告诉 ruby​​ 要加载什么文件>

    2. 将当前工作目录添加到您的加载路径:$:.push(File.dirname(__FILE__)).这样你就可以保留 require 'haiku' 部分.

    Trying to go through the tekpub rack tutorial but run into this error.

    Boot Error
    
    Something went wrong while loading app.ru
    
    LoadError: cannot load such file -- haiku
    

    There is a file named haiku.rb in the same directory as the app I am trying to run but I get the above error while trying to run the program. Here is the code:

    class EnvironmentOutput
    
    
      def initialize(app=nil)
        @app = app
      end
    
    
      def call(env)
        out = ""
    
        unless(@app.nil?)
     response = @app.call(env)[2]
     out+=response
    end
    
    env.keys.each {|key| out+="<li>#{key}=#{env[key]}</li>"}
    ["200",{"Content-Type" => "text/html"},[out]]
      end
    end
    
    require 'haml'
    require 'haiku'
    
    class MyApp
      def call(env)
    
      poem = Haiku.new.random
      template = File.open("views/index.haml").read
      engine = Haml::Engine.new(template)
      out = engine.render(Object.new, :poem => poem)
    
       ["200",{"Content-Type" => "text/html"}, out]
      end
    end
    
    use EnvironmentOutput
    run MyApp.new
    

    I'm sure its a small error as the code is the same as in the tutorial and it works for him...

    Thanks

    解决方案

    You'll want to read up on ruby load path (either $LOAD_PATH or $:). By default, ruby has a load path which includes wherever your gems are installed, which is why you can do require 'haml' without providing the full path to where your haml gem is located.

    When you type require 'haiku', you're basically telling ruby to look for some file called haiku.rb somewhere in it's load path, and the LoadError comes from ruby not finding your haiku.rb file in any of the directories listed in $LOAD_PATH (or $:, which is just shorthand for $LOAD_PATH).

    You can solve this in one of (at least) two ways:

    1. change require 'haiku' to require File.dirname(__FILE__) + '/haiku.rb' to explicitly tell ruby what file to load

    2. add the current working directory to your load path: $:.push(File.dirname(__FILE__)). This way you can keep the require 'haiku' part.

    这篇关于机架错误 -- LoadError: 无法加载此类文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    相关文章
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆