Ruby on Rails 和 Rake 问题:未初始化的常量 Rake::DSL

发布于 2024-11-09 02:48:54 字数 2337 浏览 0 评论 0原文

我遇到了一个非常令人沮丧的问题: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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(19

国际总奸 2024-11-16 02:48:54

之前发布的来自 DHH 的推文。 Rake .9.0 破坏了 Rails 和其他一些东西,您需要:

gem "rake", "0.8.7"

在您的 Gemfile 中。

A tweet from DHH earlier. Rake .9.0 breaks Rails and several other things, you need to:

gem "rake", "0.8.7"

in your Gemfile.

半夏半凉 2024-11-16 02:48:54

我在之前的回答之后做了一些研究(抱歉,我必须在回答之前做)。

所有问题都可以用 Rake gem 0.9.2 解决。我按照以下步骤操作:

  • 我安装了 gem install rake -v=0.9.2 (我有 0.9.1 gem)
  • 删除了 0.9.1 gem uninstall rake -v=0.9.1
  • 使用 bundle update 进行了更新
  • 然后 db:migrate 显示了警告, 警告:不推荐对 Rake DSL 方法进行全局访问。请....

    通过将以下内容添加到 Rake 文件中解决了这个问题。

    模块::YourApplicationName  
      类应用
        包括 Rake::DSL
      结尾
    结尾
    
  • 我省略了@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:

  • I installed gem install rake -v=0.9.2 (I had the 0.9.1 gem)
  • removed the 0.9.1 with gem uninstall rake -v=0.9.1
  • updated with 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.

    module ::YourApplicationName  
      class Application
        include Rake::DSL
      end
    end
    
  • I ommited the module ::RakeFileUtils extend Rake::FileUtilsExtend option sugested by @databyte.

It means that the Rake gem 0.9.2 works fine!

日记撕了你也走了 2024-11-16 02:48:54

浏览 Railstutorial (demo_app) 的第 2 章并遇到了这个问题。我尝试了这里列出的所有其他答案,但在我这样做之前无法让它工作:

将其放入上面的 Rakefile 中 require 'rake':

require 'rake/dsl_definition'

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':

require 'rake/dsl_definition'

via How to fix the uninitialized constant Rake::DSL problem on Heroku?

I also recommitted and pushed all files to Github and Heroku.

长发绾君心 2024-11-16 02:48:54

我需要做的就是使用:

gem install rake

我已经有了 0.9.2 版本,只需要安装。

All I needed to do was use:

gem install rake

I had version 0.9.2 already, just needed installing.

爺獨霸怡葒院 2024-11-16 02:48:54

重新安装 rake gem,它应该可以正常工作:

gem uninstall rake -v=0.9.2 
gem install rake -v=0.9.2

如果没有,请在 Gemfile 中指定版本“0.8.7”。

Reinstall the rake gem and it should work fine:

gem uninstall rake -v=0.9.2 
gem install rake -v=0.9.2

If not, specify version '0.8.7' in your Gemfile.

坐在坟头思考人生 2024-11-16 02:48:54

如果不使用 Bundler:

sudo gem install rake -v 0.8.7
sudo gem uninstall rake

则选择卸载 0.9.0。

If not using Bundler:

sudo gem install rake -v 0.8.7
sudo gem uninstall rake

Then choose to uninstall 0.9.0.

涙—继续流 2024-11-16 02:48:54

如果像我一样,你被困在 rake 0.8.7 上,并且你正在使用 Rails 3.2.x,那么 Railties 添加了对 Rake::DSL 的要求。

要解决这个问题,你应该在 Rakefile 的顶部添加:

module Rake
  module DSL
  end
end

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:

module Rake
  module DSL
  end
end
梦罢 2024-11-16 02:48:54

我通过以下步骤解决了同样的问题:

在 Gemfile 中:

gem 'rake', '0.9.2'

然后在控制台上运行:

sudo bundle update rake

然后将以下行添加到 Rakefile:

require 'rake/dsl_definition'
include Rake::DSL

I solved the same problem with the following steps:

In Gemfile:

gem 'rake', '0.9.2'

Then ran this on the console:

sudo bundle update rake

Then added the following lines to Rakefile:

require 'rake/dsl_definition'
include Rake::DSL
盗梦空间 2024-11-16 02:48:54

Rails 3.1.rc1 已更新。对于您自己的 Rakefile,您可以在调用 load_tasks 之前添加此内容。

module ::YourApplicationName
  class Application
    include Rake::DSL
  end
end

module ::RakeFileUtils
  extend Rake::FileUtilsExt
end

更新:还注意到它也已经在这里得到了回答 <一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.

module ::YourApplicationName
  class Application
    include Rake::DSL
  end
end

module ::RakeFileUtils
  extend Rake::FileUtilsExt
end

https://gist.github.com/4cd2bbe68f98f2f0249f

UPDATE: Also noticed it's already answered here as well: Undefined method 'task' using Rake 0.9.0

桃扇骨 2024-11-16 02:48:54

我遇到了同样的问题,不得不使用 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.

转身泪倾城 2024-11-16 02:48:54

我是 Windows XP 用户,也遇到了同样的问题。

我在 gemfile 中输入 gem“rake”、“0.8.7”,然后从命令窗口键入以下内容。

bundle update rake

这解决了我的问题。

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.

bundle update rake

This fixed my problem.

苍暮颜 2024-11-16 02:48:54
  1. 转到您的项目路径,
  2. 键入 bundle install --path=vendor/bundle
  3. 键入 bundle exec rake db:migrate

要启动服务器,键入 bundle execrails s >。使用bundle exec,您将确保为您的项目使用正确的gems(所需版本)。
另外,如果您使用 git 并为 bundle exec 创建别名,我建议您将 vendor/bundle 添加到 .gitignore >。如果您使用zsh,您可以按照这种方法

  1. Go to your project path
  2. Type bundle install --path=vendor/bundle
  3. Type 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 use git and make alias for bundle exec. If you use zsh you can follow this approach

吃素的狼 2024-11-16 02:48:54

与上面的布兰斯塔相同 - 谢谢布兰斯塔!

  • 操作系统: Windows Vista
  • 级别: Ruby on Rails 全新
  • 我已经安装了 Ruby 1.9.2

我按照 在 Windows 上运行 Rails 3

一切都在进行,直到“rake db:migrate”部分给了我与原始帖子相同的输出。

我跑了:

gem install rake

我又跑了:

rake db:migrate

然后我就能够启动 Ruby on Rails 服务器并且一切就绪。

再次感谢布兰斯塔:-)

Same as Branstar above - thanks Branstar!

  • OS: Windows Vista
  • Level: Completely new to Ruby on Rails
  • I already had Ruby 1.9.2 installed

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:

gem install rake

I ran again:

rake db:migrate

Then I was able to start the Ruby on Rails server and had everything in place.

Thanks again Branstar :-)

﹂绝世的画 2024-11-16 02:48:54

我为你感到难过(mikhailvs),这真的很令人沮丧。我已经快疯了整整一天了。我什至卸载了 Ruby 及其所有依赖文件并关闭了我的电脑,但我仍然遇到同样的问题。

我从错误消息中得到的是Rake 0.9.2的问题。好像没有完全安装。所以我不得不重新安装 gem install rake -v=0.9.2

我不确定是否安装了 rake –v0.9.1。因此,为了确保我的安全,我尝试使用 gem uninstall rake -v=0.9.1 删除旧版本。但是向我显示了错误消息

ERROR:  While executing gem ... (Gem::InstallError)
    cannot uninstall, check `gem list -d rake`

“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 message

ERROR:  While executing gem ... (Gem::InstallError)
    cannot uninstall, check `gem list -d rake`

OK, 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.

北风几吹夏 2024-11-16 02:48:54

使用“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.

樱娆 2024-11-16 02:48:54

我在使用 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.

迷爱 2024-11-16 02:48:54

对于 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

伏妖词 2024-11-16 02:48:54

安装 rake 0.8.7 并卸载 0.9.2.2

$ gem install rake -v 0.8.7
$ gem uninstall rake -v 0.9.2.2

现在使用

$ bundle exec rake db:migrate

我认为这会对您有所帮助
;)

Install rake 0.8.7 and uninstall 0.9.2.2

$ gem install rake -v 0.8.7
$ gem uninstall rake -v 0.9.2.2

Now use

$ bundle exec rake db:migrate

i think this will help you
;)

当梦初醒 2024-11-16 02:48:54

运行

bundle exec rake db:migrate

它对我有用。

Run

bundle exec rake db:migrate

it works for me.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文