Ruby on Rails 和 Rake 问题:未初始化的常量 Rake::DSL
我遇到了一个非常令人沮丧的问题:Rake 很愚蠢。
问题是这样产生的:
$ rails new test_app
$ rails generate scaffold new_scaffold field1:string field2:text
这两个都工作得很好,但是当我这样做时,
$ rake db:migrate
我收到以下错误。
(in /home/mikhail/test_app)
rake aborted!
uninitialized constant Rake::DSL
/usr/lib/ruby/1.9.1/rake.rb:2482:in `const_missing'
/usr/lib/ruby/gems/1.9.1/gems/rake-0.9.0/lib/rake/tasklib.rb:8:in `<class:TaskLib>'
/usr/lib/ruby/gems/1.9.1/gems/rake-0.9.0/lib/rake/tasklib.rb:6:in `<module:Rake>'
/usr/lib/ruby/gems/1.9.1/gems/rake-0.9.0/lib/rake/tasklib.rb:3:in `<top (required)>'
/usr/lib/ruby/gems/1.9.1/gems/rake-0.9.0/lib/rake/rdoctask.rb:20:in `require'
/usr/lib/ruby/gems/1.9.1/gems/rake-0.9.0/lib/rake/rdoctask.rb:20:in `<top (required)>'
/usr/lib/ruby/gems/1.9.1/gems/railties-3.0.7/lib/rails/tasks/documentation.rake:1:in `require'
/usr/lib/ruby/gems/1.9.1/gems/railties-3.0.7/lib/rails/tasks/documentation.rake:1:in `<top (required)>'
/usr/lib/ruby/gems/1.9.1/gems/railties-3.0.7/lib/rails/tasks.rb:15:in `load'
/usr/lib/ruby/gems/1.9.1/gems/railties-3.0.7/lib/rails/tasks.rb:15:in `block in <top (required)>'
/usr/lib/ruby/gems/1.9.1/gems/railties-3.0.7/lib/rails/tasks.rb:6:in `each'
/usr/lib/ruby/gems/1.9.1/gems/railties-3.0.7/lib/rails/tasks.rb:6:in `<top (required)>'
/usr/lib/ruby/gems/1.9.1/gems/railties-3.0.7/lib/rails/application.rb:214:in `require'
/usr/lib/ruby/gems/1.9.1/gems/railties-3.0.7/lib/rails/application.rb:214:in `initialize_tasks'
/usr/lib/ruby/gems/1.9.1/gems/railties-3.0.7/lib/rails/application.rb:139:in `load_tasks'
/usr/lib/ruby/gems/1.9.1/gems/railties-3.0.7/lib/rails/application.rb:77:in `method_missing'
/home/mikhail/test_app/Rakefile:7:in `<top (required)>'
/usr/lib/ruby/1.9.1/rake.rb:2373:in `load'
/usr/lib/ruby/1.9.1/rake.rb:2373:in `raw_load_rakefile'
/usr/lib/ruby/1.9.1/rake.rb:2007:in `block in load_rakefile'
/usr/lib/ruby/1.9.1/rake.rb:2058:in `standard_exception_handling'
/usr/lib/ruby/1.9.1/rake.rb:2006:in `load_rakefile'
/usr/lib/ruby/1.9.1/rake.rb:1991:in `run'
/usr/bin/rake:31:in `<main>'
我在互联网上查找过类似/相同的错误,人们也遇到过这些错误。只是似乎没有人能够解决这个问题!
我该如何解决这个问题?
I'm having a really frustrating issue: Rake is being dumb.
Here's how the problem comes about:
$ rails new test_app
$ rails generate scaffold new_scaffold field1:string field2:text
Both of those work just fine, but then when I do this,
$ rake db:migrate
I get the following error.
(in /home/mikhail/test_app)
rake aborted!
uninitialized constant Rake::DSL
/usr/lib/ruby/1.9.1/rake.rb:2482:in `const_missing'
/usr/lib/ruby/gems/1.9.1/gems/rake-0.9.0/lib/rake/tasklib.rb:8:in `<class:TaskLib>'
/usr/lib/ruby/gems/1.9.1/gems/rake-0.9.0/lib/rake/tasklib.rb:6:in `<module:Rake>'
/usr/lib/ruby/gems/1.9.1/gems/rake-0.9.0/lib/rake/tasklib.rb:3:in `<top (required)>'
/usr/lib/ruby/gems/1.9.1/gems/rake-0.9.0/lib/rake/rdoctask.rb:20:in `require'
/usr/lib/ruby/gems/1.9.1/gems/rake-0.9.0/lib/rake/rdoctask.rb:20:in `<top (required)>'
/usr/lib/ruby/gems/1.9.1/gems/railties-3.0.7/lib/rails/tasks/documentation.rake:1:in `require'
/usr/lib/ruby/gems/1.9.1/gems/railties-3.0.7/lib/rails/tasks/documentation.rake:1:in `<top (required)>'
/usr/lib/ruby/gems/1.9.1/gems/railties-3.0.7/lib/rails/tasks.rb:15:in `load'
/usr/lib/ruby/gems/1.9.1/gems/railties-3.0.7/lib/rails/tasks.rb:15:in `block in <top (required)>'
/usr/lib/ruby/gems/1.9.1/gems/railties-3.0.7/lib/rails/tasks.rb:6:in `each'
/usr/lib/ruby/gems/1.9.1/gems/railties-3.0.7/lib/rails/tasks.rb:6:in `<top (required)>'
/usr/lib/ruby/gems/1.9.1/gems/railties-3.0.7/lib/rails/application.rb:214:in `require'
/usr/lib/ruby/gems/1.9.1/gems/railties-3.0.7/lib/rails/application.rb:214:in `initialize_tasks'
/usr/lib/ruby/gems/1.9.1/gems/railties-3.0.7/lib/rails/application.rb:139:in `load_tasks'
/usr/lib/ruby/gems/1.9.1/gems/railties-3.0.7/lib/rails/application.rb:77:in `method_missing'
/home/mikhail/test_app/Rakefile:7:in `<top (required)>'
/usr/lib/ruby/1.9.1/rake.rb:2373:in `load'
/usr/lib/ruby/1.9.1/rake.rb:2373:in `raw_load_rakefile'
/usr/lib/ruby/1.9.1/rake.rb:2007:in `block in load_rakefile'
/usr/lib/ruby/1.9.1/rake.rb:2058:in `standard_exception_handling'
/usr/lib/ruby/1.9.1/rake.rb:2006:in `load_rakefile'
/usr/lib/ruby/1.9.1/rake.rb:1991:in `run'
/usr/bin/rake:31:in `<main>'
I've looked about the Internet for similar/same errors, and people have had them. Just no one ever seems to solve the problem!
How do I fix this problem?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(19)
之前发布的来自 DHH 的推文。 Rake .9.0 破坏了 Rails 和其他一些东西,您需要:
在您的 Gemfile 中。
A tweet from DHH earlier. Rake .9.0 breaks Rails and several other things, you need to:
in your Gemfile.
我在之前的回答之后做了一些研究(抱歉,我必须在回答之前做)。
所有问题都可以用 Rake gem 0.9.2 解决。我按照以下步骤操作:
gem install rake -v=0.9.2
(我有 0.9.1 gem)gem uninstall rake -v=0.9.1
bundle update
进行了更新然后
db:migrate
显示了警告,警告:不推荐对 Rake DSL 方法进行全局访问。请....
通过将以下内容添加到 Rake 文件中解决了这个问题。
我省略了@databyte建议的
module ::RakeFileUtils extends Rake::FileUtilsExtend
选项。这意味着 Rake gem 0.9.2 工作正常!
I made some research just after my previous answer (sorry, I must do before it).
All problems are solved with Rake gem 0.9.2.. I followed these steps:
gem install rake -v=0.9.2
(I had the 0.9.1 gem)gem uninstall rake -v=0.9.1
bundle update
then the
db:migrate
showed a warning,WARNING: Global access to Rake DSL methods is deprecated. Please....
It was solved by adding the following to the Rake file.
I ommited the
module ::RakeFileUtils extend Rake::FileUtilsExtend
option sugested by @databyte.It means that the Rake gem 0.9.2 works fine!
浏览 Railstutorial (demo_app) 的第 2 章并遇到了这个问题。我尝试了这里列出的所有其他答案,但在我这样做之前无法让它工作:
将其放入上面的 Rakefile 中 require 'rake':
via 如何修复 Heroku 上未初始化常量 Rake::DSL 问题?
我也重新提交并推送所有文件到 Github 和 Heroku。
Going through Chapter 2 of Railstutorial (demo_app) and ran into this problem. I tried all of the other answers listed here, but couldn't get it to work until I did this:
Put this in your Rakefile above require 'rake':
via How to fix the uninitialized constant Rake::DSL problem on Heroku?
I also recommitted and pushed all files to Github and Heroku.
我需要做的就是使用:
我已经有了 0.9.2 版本,只需要安装。
All I needed to do was use:
I had version 0.9.2 already, just needed installing.
重新安装 rake gem,它应该可以正常工作:
如果没有,请在 Gemfile 中指定版本“0.8.7”。
Reinstall the rake gem and it should work fine:
If not, specify version '0.8.7' in your Gemfile.
如果不使用 Bundler:
则选择卸载 0.9.0。
If not using Bundler:
Then choose to uninstall 0.9.0.
如果像我一样,你被困在 rake 0.8.7 上,并且你正在使用 Rails 3.2.x,那么 Railties 添加了对 Rake::DSL 的要求。
要解决这个问题,你应该在 Rakefile 的顶部添加:
If like me you're stuck on rake 0.8.7, and you're using Rails 3.2.x then railties adds a requirement for Rake::DSL
To solve this, to the top of your Rakefile you should add:
我通过以下步骤解决了同样的问题:
在 Gemfile 中:
然后在控制台上运行:
然后将以下行添加到 Rakefile:
I solved the same problem with the following steps:
In Gemfile:
Then ran this on the console:
Then added the following lines to Rakefile:
Rails 3.1.rc1 已更新。对于您自己的 Rakefile,您可以在调用 load_tasks 之前添加此内容。
:
更新:还注意到它也已经在这里得到了回答 <一href="https://stackoverflow.com/questions/5287121/undefined-method-task-using-rake-0-9-0-beta-4/5290331#5290331">使用 Rake 0.9.0 的未定义方法“任务”
Rails 3.1.rc1 has been updated. For your own Rakefiles, you can add this before the call to load_tasks.
https://gist.github.com/4cd2bbe68f98f2f0249f
UPDATE: Also noticed it's already answered here as well: Undefined method 'task' using Rake 0.9.0
我遇到了同样的问题,不得不使用 rake 0.8.7 gem 而不是 0.9.0。
I had the same issue and had to use the rake 0.8.7 gem instead of 0.9.0.
我是 Windows XP 用户,也遇到了同样的问题。
我在 gemfile 中输入 gem“rake”、“0.8.7”,然后从命令窗口键入以下内容。
这解决了我的问题。
I am a Windows XP user and I had the same problem.
I entered gem "rake", "0.8.7" into the gemfile, and then typed the following from the command window.
This fixed my problem.
bundle install --path=vendor/bundle
bundle exec rake db:migrate
要启动服务器,键入
bundle execrails s
>。使用bundle exec,您将确保为您的项目使用正确的gems(所需版本)。另外,如果您使用
git
并为bundle exec
创建别名,我建议您将vendor/bundle
添加到.gitignore
>。如果您使用zsh
,您可以按照这种方法bundle install --path=vendor/bundle
bundle exec rake db:migrate
To start server type
bundle exec rails s
. Use bundle exec and you will be sure that you use right gems (required version) for your project.Also I would recommend you to add
vendor/bundle
to.gitignore
if you usegit
and make alias forbundle exec
. If you usezsh
you can follow this approach与上面的布兰斯塔相同 - 谢谢布兰斯塔!
我按照 在 Windows 上运行 Rails 3。
一切都在进行,直到“rake db:migrate”部分给了我与原始帖子相同的输出。
我跑了:
我又跑了:
然后我就能够启动 Ruby on Rails 服务器并且一切就绪。
再次感谢布兰斯塔:-)
Same as Branstar above - thanks Branstar!
I followed the instructions in Running Rails 3 on Windows.
All worked up until the "rake db:migrate" part which gave me the same output as original post.
I ran:
I ran again:
Then I was able to start the Ruby on Rails server and had everything in place.
Thanks again Branstar :-)
我为你感到难过(mikhailvs),这真的很令人沮丧。我已经快疯了整整一天了。我什至卸载了 Ruby 及其所有依赖文件并关闭了我的电脑,但我仍然遇到同样的问题。
我从错误消息中得到的是Rake 0.9.2的问题。好像没有完全安装。所以我不得不重新安装
gem install rake -v=0.9.2
我不确定是否安装了 rake –v0.9.1。因此,为了确保我的安全,我尝试使用
gem uninstall rake -v=0.9.1
删除旧版本。但是向我显示了错误消息“OK”,所以我检查了我的电脑上的所有 Rake 目录,发现我只有 Rake 0.9.2。
然后为了检查一切是否正常,我使用
rake db:migrate
进行了迁移。它起作用了:)我想我没有 Rake 0.9.1 因为我干净安装了 Ruby (rubyinstaller-1.9.2-p180 - 在我的 Windows 7 系统上)以及所有 gem。与此同时,Rake 0.9.2 尚未完全安装。
I feel for you (mikhailvs), it's really frustrating. I have been going crazy for almost one full day. I even uninstalled Ruby and all its dependent files and shutdown my PC, but I still got the same problem.
What I got from the error message is the problem with Rake 0.9.2. It seems like it wasn’t fully installed. So I had to reinstall
gem install rake -v=0.9.2
I wasn’t sure if I have rake –v0.9.1 installed. So to make sure I’m safe I tried to remove that old version with
gem uninstall rake -v=0.9.1
. But is showed me the error messageOK, so I checked all Rake directories on my PC, and found I only had Rake 0.9.2.
Then to check if everything went alright, I migrated with
rake db:migrate
. And it worked :)I think I didn’t have Rake 0.9.1 because I clean-installed Ruby (rubyinstaller-1.9.2-p180 - on my Windows 7 system) and all gems as well. In the meantime Rake 0.9.2 wasn’t fully installed.
使用“gem uninstall rake”卸载对我有用,我安装了2个版本,所以我开玩笑地进行了一次干净的重新安装。
“rake db:create”,确保数据库存在
然后“rake db:migrate”来达成交易。
Uninstalling with "gem uninstall rake" worked for me, I had 2 versions installed, so I jest did a clean reinstall.
"rake db:create", to make sure the database exists
and then "rake db:migrate" to seal the deal.
我在使用 Rake 0.9.2.2 时遇到了同样的问题。我通过使用bundle exec解决了这个问题。
I had the same issue using Rake 0.9.2.2. I solved this problem by using bundle exec.
对于 Rails 2.3 编辑
lib/tasks/rspec.rake
就像这个提交对我有用:For Rails 2.3 editing
lib/tasks/rspec.rake
like in this commit worked for me:https://github.com/dchelimsky/rspec-rails/pull/11/files
安装 rake 0.8.7 并卸载 0.9.2.2
现在使用
我认为这会对您有所帮助
;)
Install rake 0.8.7 and uninstall 0.9.2.2
Now use
i think this will help you
;)
运行
它对我有用。
Run
it works for me.