gpt4 book ai didi

rake - Rake 任务中未初始化的常量

转载 作者:行者123 更新时间:2023-12-03 06:57:08 33 4
gpt4 key购买 nike

设置如下:

新建 Rails 应用程序,然后将此 test_rake.rake 放入 lib/tasks 中:

task :testclass do
HelloClass.hello
end`

将 hello_class.rb 放入 app/models 或 lib/中,并添加以下行:config.autoload_paths += %W(#{config.root}/lib) 添加到 config.rb

class HelloClass
def self.hello
puts 'hello_class'
end
end

rake testclass 给出此错误:

/Users/name/.rvm/gems/ruby-1.9.2-p0@global/gems/rake-0.8.7/lib/rake.rb:2503:in `const_missing'
/Users/name/Sites/Rails/indexapp/lib/tasks/test_class.rake:5:in `block (2 levels) in <top (required)>'
/Users/name/.rvm/gems/ruby-1.9.2-p0@global/gems/rake-0.8.7/lib/rake.rb:636:in `call'
/Users/name/.rvm/gems/ruby-1.9.2-p0@global/gems/rake-0.8.7/lib/rake.rb:636:in `block in execute'
/Users/name/.rvm/gems/ruby-1.9.2-p0@global/gems/rake-0.8.7/lib/rake.rb:631:in `each'
/Users/name/.rvm/gems/ruby-1.9.2-p0@global/gems/rake-0.8.7/lib/rake.rb:631:in `execute'
/Users/name/.rvm/gems/ruby-1.9.2-p0@global/gems/rake-0.8.7/lib/rake.rb:597:in `block in invoke_with_call_chain'
/Users/name/.rvm/rubies/ruby-1.9.2-p0/lib/ruby/1.9.1/monitor.rb:201:in `mon_synchronize'
/Users/name/.rvm/gems/ruby-1.9.2-p0@global/gems/rake-0.8.7/lib/rake.rb:590:in `invoke_with_call_chain'
/Users/name/.rvm/gems/ruby-1.9.2-p0@global/gems/rake-0.8.7/lib/rake.rb:583:in `invoke'
/Users/name/.rvm/gems/ruby-1.9.2-p0@global/gems/rake-0.8.7/lib/rake.rb:2051:in `invoke_task'
/Users/name/.rvm/gems/ruby-1.9.2-p0@global/gems/rake-0.8.7/lib/rake.rb:2029:in `block (2 levels) in top_level'
/Users/name/.rvm/gems/ruby-1.9.2-p0@global/gems/rake-0.8.7/lib/rake.rb:2029:in `each'
/Users/name/.rvm/gems/ruby-1.9.2-p0@global/gems/rake-0.8.7/lib/rake.rb:2029:in `block in top_level'
/Users/name/.rvm/gems/ruby-1.9.2-p0@global/gems/rake-0.8.7/lib/rake.rb:2068:in `standard_exception_handling'
/Users/name/.rvm/gems/ruby-1.9.2-p0@global/gems/rake-0.8.7/lib/rake.rb:2023:in `top_level'
/Users/name/.rvm/gems/ruby-1.9.2-p0@global/gems/rake-0.8.7/lib/rake.rb:2001:in `block in run'
/Users/name/.rvm/gems/ruby-1.9.2-p0@global/gems/rake-0.8.7/lib/rake.rb:2068:in `standard_exception_handling'
/Users/name/.rvm/gems/ruby-1.9.2-p0@global/gems/rake-0.8.7/lib/rake.rb:1998:in `run'
/Users/name/.rvm/gems/ruby-1.9.2-p0@global/gems/rake-0.8.7/bin/rake:31:in `<top (required)>'
/Users/name/.rvm/gems/ruby-1.9.2-p0@global/bin/rake:19:in `load'
/Users/name/.rvm/gems/ruby-1.9.2-p0@global/bin/rake:19:in `<main>'

有什么想法吗?我已经通过 RVM 卸载并重新安装了 Ruby,删除/重建了 gemset,打印出自动加载路径以确保 hello_class.rb 位于其中之一...

我可以在 .rake 文件中手动请求 HelloClass,但随后我必须对 HelloClass 所依赖的任何内容执行相同的操作 - 例如,如果 HelloClass 包含 HTTParty 或任务设置了延迟作业。

任何帮助都会很棒。谢谢!

最佳答案

如果您使用 task :testclass => :environment do 启动 rake 任务,您的 Rails 环境将被加载并可用于该任务。

关于rake - Rake 任务中未初始化的常量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4572507/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com