Ruby-on-rails 机架错误 -- LoadError: 无法加载此类文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7702980/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 02:08:21  来源:igfitidea点击:

Rack Error -- LoadError: cannot load such file

ruby-on-railsrubyrack

提问by AFraser

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

试图通过 tekpub 机架教程但遇到此错误。

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:

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

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

谢谢

回答by Frost

You'll want to read up on ruby load path (either $LOAD_PATHor $:). 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.

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

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

当您键入 时require 'haiku',您基本上是在告诉 ruby​​ 查找haiku.rb在其加载路径中某处调用的某个文件,并且LoadError来自 ruby​​ 未haiku.rb$LOAD_PATH(or ) 中列出的任何目录中找到您的文件$:,这只是 的简写$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.

  1. 更改require 'haiku'require File.dirname(__FILE__) + '/haiku.rb'明确告诉 ruby​​ 要加载哪个文件

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