Ruby-on-rails 如何修复 Heroku 上未初始化的常量 Rake::DSL 问题?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6181312/
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
How to fix the uninitialized constant Rake::DSL problem on Heroku?
提问by ben
I am getting errors similar to the ones inthesequestions, except mine are occuring on Heroku:
我越来越类似于那些错误的这些问题,除了我的是上发生的Heroku:
2011-05-30T09:03:29+00:00 heroku[worker.1]: Starting process with command: `rake jobs:work`
2011-05-30T09:03:30+00:00 app[worker.1]: (in /app)
2011-05-30T09:03:30+00:00 heroku[worker.1]: State changed from starting to up
2011-05-30T09:03:33+00:00 app[worker.1]: rake aborted!
2011-05-30T09:03:33+00:00 app[worker.1]: uninitialized constant Rake::DSL
2011-05-30T09:03:33+00:00 app[worker.1]: /app/.bundle/gems/ruby/1.9.1/gems/rake-0.9.0/lib/rake/tasklib.rb:8:in `<class:TaskLib>'
The answer in those questions seems to be to specify gem 'rake', '0.8.7'because the 0.9 version causes the problem.
这些问题的答案似乎是指定,gem 'rake', '0.8.7'因为 0.9 版本导致了问题。
When I try to add gem 'rake', '0.8.7'to my gemfile and push to Heroku I get this error:
当我尝试添加gem 'rake', '0.8.7'到我的 gemfile 并推送到 Heroku 时,我收到此错误:
Unresolved dependencies detected; Installing...
You have modified your Gemfile in development but did not check
the resulting snapshot (Gemfile.lock) into version control
You have added to the Gemfile:
* rake (= 0.8.7)
FAILED: http://devcenter.heroku.com/articles/bundler
! Heroku push rejected, failed to install gems via Bundler
error: hooks/pre-receive exited with error code 1
To [email protected]:my_app.git
! [remote rejected] master -> master (pre-receive hook declined)
error: failed to push some refs to '[email protected]:my_app.git'
My gemfile normally works fine on Heroku. What should I do?
我的 gemfile 通常在 Heroku 上运行良好。我该怎么办?
回答by Kale
Put this in your Rakefile aboverequire 'rake':
把它放在你的 Rakefile上面require 'rake':
require 'rake/dsl_definition'
回答by wuputah
Any time you change your Gemfile, you need to bundle installto update your lockfile (Gemfile.lock). The error you're getting on push is not specific to changing the version of rake.
任何时候你改变你的 Gemfile,你都需要bundle install更新你的锁文件 (Gemfile.lock)。您在推送时遇到的错误并非特定于更改 rake 版本。
bundle install
git commit -a -m "update lockfile"
git push heroku master
Note the error message you received:
请注意您收到的错误消息:
You have modified your Gemfile in development but did not check the resulting snapshot (Gemfile.lock) into version control
您在开发中修改了 Gemfile,但没有将生成的快照 (Gemfile.lock) 检入版本控制
回答by Max Williams
I solved this, finally, after a lot of mucking about. The short version of what I did, missing out the many experiments, was this:
经过一番折腾,我终于解决了这个问题。我所做的事情的简短版本,错过了许多实验,是这样的:
1) change the Gemfile to specify Rake 0.8.7
1) 更改 Gemfile 以指定 Rake 0.8.7
#in Gemfile
gem "rake", "0.8.7"
2) Take out a hack that I had previously added to Rakefile based on Stack Overflow question Ruby on Rails and Rake problems: uninitialized constant Rake::DSL:
2) 根据 Stack Overflow 问题Ruby on Rails 和 Rake 问题:未初始化的常量 Rake::DSL删除我之前添加到 Rakefile 的 hack :
So, my Rakefile is now back to being the standard Rakefile for my app:
所以,我的 Rakefile 现在重新成为我的应用程序的标准 Rakefile:
# Add your own tasks in files placed in lib/tasks ending in .rake,
# for example lib/tasks/capistrano.rake, and they will automatically be available to Rake.
require File.expand_path('../config/application', __FILE__)
require 'rake'
MyApp::Application.load_tasks
3) Change Heroku to run my app in Ruby 1.9.2:
3) 更改 Heroku 以在 Ruby 1.9.2 中运行我的应用程序:
heroku stack:migrate bamboo-mri-1.9.2 --app myapp
git push heroku master
And it seems fine now - the scheduled cron task is running anyway.
现在看起来很好 - 计划的 cron 任务无论如何都在运行。
EDIT: It didrun fine, once, then blew up again next time I pushed something! Arrgh. I think I fixed it now, with the addition of the delayed_jobgem, based on the conversation Don't know how to build task jobs:work.
编辑:它确实运行良好,一次,然后下次我推东西时再次爆炸!啊。我想我现在修复了它,添加了delayed_jobgem,基于对话Don't know how to build task jobs:work。
Installing delayed_jobdoesn't seem like a great solution, but it HAS worked, and I might want to use it sometime I guess, especially with Heroku's once-per-hour cron job (which just isn't frequent enough - there are things I'll probably want to run every five minutes). After I installed the delayed_jobgem I had to do the setup for it, otherwise Heroku complains about the missing delayed_jobstable:
安装delayed_job似乎不是一个很好的解决方案,但它已经奏效了,我想我可能想在某个时候使用它,尤其是 Heroku 每小时一次的 cron 作业(这只是不够频繁 - 有些事情我我可能想每五分钟运行一次)。安装delayed_jobgem 后,我必须为它进行设置,否则 Heroku 会抱怨缺少delayed_jobs表:
#add to gemfile
gem 'delayed_job'
#at command line
bundle install
rails g delayed_job
rake db:migrate
git add -A
git commit -a -m "added delayed_job gem"
git push
heroku rake db:migrate --app myapp
heroku restart --app myapp
回答by Wes
I had a Rails 3.0.11 app, which specified rake version 0.8.7 in the Gemfile to get around the version 0.9.2 Rake::DSL problem.
我有一个 Rails 3.0.11 应用程序,它在 Gemfile 中指定了 rake 版本 0.8.7 来解决版本 0.9.2 Rake::DSL 问题。
After I converted the app to Rails 3.2.0 (Heroku Cedar stack), I was having a problem with the worker (a rake task) crashing. I changed "gem 'rake', '0.8.7'" to "gem 'rake'", which bundled rake version 0.9.2.2. The worker stopped crashing with the new version.
在我将应用程序转换为 Rails 3.2.0(Heroku Cedar 堆栈)后,我遇到了 worker(一个 rake 任务)崩溃的问题。我将“gem 'rake', '0.8.7'”改为“gem 'rake'”,它捆绑了 rake 版本 0.9.2.2。工作人员停止使用新版本崩溃。
回答by klaffenboeck
Your problem is caused by not deleting the Gemfile.lockfile and is not specific to Heroku. Deleting Gemfile.lockshould fix this problem, but will lead you straight to another one:
您的问题是由于未删除Gemfile.lock文件引起的,并且不是 Heroku 特有的。删除Gemfile.lock应该可以解决这个问题,但会直接带你到另一个问题:
To [email protected]:tailored-landing-pages.git
* [new branch] master -> master
manfred@painstation2:~/Desktop/projects/ror/ta/tlp307$ heroku rake db:migrate
rake aborted!
ninitialized constant Rake::DSL
/app/Rakefile:13:in `<class:Application>'
/app/Rakefile:12:in `<module:Tlp307>'
/app/Rakefile:11:in `<top (required)>'
/usr/ruby1.9.2/lib/ruby/1.9.1/rake.rb:2373:in `load'
/usr/ruby1.9.2/lib/ruby/1.9.1/rake.rb:2373:in `raw_load_rakefile'
/usr/ruby1.9.2/lib/ruby/1.9.1/rake.rb:2007:in `block in load_rakefile'
/usr/ruby1.9.2/lib/ruby/1.9.1/rake.rb:2058:in `standard_exception_handling'
/usr/ruby1.9.2/lib/ruby/1.9.1/rake.rb:2006:in `load_rakefile'
/usr/ruby1.9.2/lib/ruby/1.9.1/rake.rb:1991:in `run'
/usr/ruby1.9.2/bin/rake:31:in `<main>'
Unfortunately, I haven't found the solution for that problem yet, since downgrading Rake to 0.8.7 doesn't seem to work here. If somebody else has an answer, I would appreciate it very much.
不幸的是,我还没有找到该问题的解决方案,因为将 Rake 降级到 0.8.7 在这里似乎不起作用。如果其他人有答案,我将不胜感激。

