简体   繁体   English

rake:rake任务中的未初始化常量

[英]rake: uninitialized constant in rake task

I have some custom classes/modules defined under /app/lib . 我在/app/lib下定义了一些自定义类/模块。

I have a Rake task, via whenever, that tries to load the environment then execute one of the class methods. 我有一个Rake任务,该任务随时尝试加载环境,然后执行其中一个类方法。

Here's an example of the rake task: 这是rake任务的示例:

namespace :box do
  task :fetch => :environment do
  BoxInterface::Tasc::Fetcher.fetch
  end
end

If I run BoxInterface::Tasc::Fetcher.fetch from the Rails console, it works fine. 如果我从Rails控制台运行BoxInterface::Tasc::Fetcher.fetch ,它将正常工作。

If I run bundle exec rake 'box:fetch' I get this error: 如果我运行bundle exec rake 'box:fetch' ,则会出现此错误:

uninitialized constant BoxInterface::Tasc

The file structure under lib is: lib下的文件结构为:

/app/lib/box_interface/tasc/fetcher.rb

The odd thing here is that we have a staging server that this code works fine under. 奇怪的是,我们有一个登台服务器,该代码可以在该服务器上正常工作。 Does this have something to do with the environment? 这与环境有关吗? I am unsure how to troubleshoot this. 我不确定如何解决此问题。

Have you added the folders in your lib directory to your autoload path in your application.rb file? 您是否已将lib目录中的文件夹添加到application.rb文件中的自动加载路径中? You might have to do that if you're using Rails 3.0 or greater. 如果您使用的是Rails 3.0或更高版本,则可能必须这样做。 Try adding this line: 尝试添加以下行:

config.autoload_paths += %W(#{Rails.root}/lib/box_interface/tasc)

inside of your class definition ( class Application < Rails::Application ) in the application.rb file. application.rb文件中的类定义( class Application < Rails::Application )中。

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

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