简体   繁体   English

`const_missing':未初始化的常量(NameError)

[英]`const_missing': uninitialized constant (NameError)

Every time I try to run any class from my rails 2.2 app's lib directory using "script/runner -e production ClassName.run" I get the following error:每次我尝试使用“script/runner -e production ClassName.run”从我的 rails 2.2 应用程序的 lib 目录中运行任何 class 时,都会出现以下错误:

/usr/lib/ruby/gems/1.8/gems/rails-2.2.2/lib/commands/runner.rb:47:  
/usr/lib/ruby/gems/1.8/gems/activesupport-2.2.2/lib/active_support/dependencies.rb:89:in `const_missing': uninitialized constant ClassName (NameError)"

For some reason I don't get this error when I run it as test instead of production , and everything works fine.出于某种原因,当我将其作为test而不是production运行时,我没有收到此错误,并且一切正常。

O yeah run is a class method ie def self.run.... end哦,是的run是一种 class 方法,即def self.run.... end

Any suggestions?有什么建议么?

That error occurs when ruby can't find a Class or Module.当 ruby 找不到 Class 或模块时,会发生该错误。 I'd start out by:我会开始:

  1. Checking gem dependencies (are they same for all environments?)检查 gem 依赖项(所有环境都相同吗?)
  2. Search your code for anything that defines ClassName, particularly Modules, Classes and plugins.在您的代码中搜索任何定义 ClassName 的内容,尤其是模块、类和插件。
  3. disable each of your plugins in dev, does any plugin suddenly cause that error?禁用开发中的每个插件,是否有任何插件突然导致该错误?
  4. if the code is in a lib add the lib require statement to your production.rb to force the lib to be loaded in production.如果代码在 lib 中,请将 lib require 语句添加到您的 production.rb 以强制在生产中加载 lib。

Hope that helps.希望有帮助。

update Just to summarise the comments it was option 4.更新只是为了总结意见,它是选项 4。

Unless you only want to load the lib in production you should think about making sure all environments load the lib by doing one of the following:除非您只想在生产中加载 lib,否则您应该考虑通过执行以下操作之一来确保所有环境都加载 lib:

  1. Create a rails initializer (a.rb file under config/initializers) with the require in it创建一个带有 require 的 rails 初始化程序(config/initializers 下的 a.rb 文件)
  2. Add the path to the lib in config/environment.rb by enabling and modifying the config.load_paths variable.通过启用和修改 config.load_paths 变量,将路径添加到 config/environment.rb 中的 lib。

I had multiple class definition(all STI classes) on the same file.我在同一个文件中有多个 class 定义(所有 STI 类)。 and when I separated the definition into their respective files, It works当我将定义分成各自的文件时,它可以工作

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM