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

HRÓ*_*LFR 190 rake ruby-on-rails

我有一个非常令人沮丧的问题:耙子是愚蠢的.

以下是问题的解决方法:

$ rails new test_app
$ rails generate scaffold new_scaffold field1:string field2:text
Run Code Online (Sandbox Code Playgroud)

这两个工作都很好,但是当我这样做时,

$ rake db:migrate
Run Code Online (Sandbox Code Playgroud)

我收到以下错误.

(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>'
Run Code Online (Sandbox Code Playgroud)

我看过互联网上类似/相同的错误,人们已经有了它们.似乎没有人似乎解决了这个问题!

我该如何解决这个问题?

Cal*_*ods 193

来自DHH推文.Rake .9.0打破了Rails和其他一些东西,你需要:

gem "rake", "0.8.7"
Run Code Online (Sandbox Code Playgroud)

在你的Gemfile中.

  • 甚至更好:`gem"rake","!= 0.9.0"`.这样可以避免破坏0.9.0版本,但是一旦发布了更新版本就会更新到更新版本(稍后删除它不会有任何损害,但保持它不会受到伤害). (26认同)
  • 这对我有用.我需要先做'捆绑更新rake`.然后我用`bundle show rake`检查了一下.应该说0.8.7.然后`rake db:migrate`. (17认同)
  • @Spiralis我也遇到了0.9.2版本的问题.因此,您可能需要为几个版本添加该行. (6认同)
  • 这个解决方案**工作**为我工作.就像@dharmatech一样,我在`heroku rake db:migrate`中出现了这个问题,这似乎解决了这个问题.记得在继续之前添加这些更改并将其提交给git;) (3认同)

小智 72

我在之前的回答之后做了一些研究(对不起,我必须在它之前做).

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

  • 我安装了gem install rake -v=0.9.2(我有0.9.1宝石)
  • 删除了0.9.1 gem uninstall rake -v=0.9.1
  • 更新了 bundle update
  • 然后db:migrate显示警告,WARNING: Global access to Rake DSL methods is deprecated. Please....

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

    module ::YourApplicationName  
      class Application
        include Rake::DSL
      end
    end
    
    Run Code Online (Sandbox Code Playgroud)
  • 我省略了@databyte提出的module ::RakeFileUtils extend Rake::FileUtilsExtend选项.

这意味着Rake gem 0.9.2工作正常!

  • 同样的问题,但我已经有了rake(0.9.2,0.8.7).重新安装0.9.2似乎解决了问题:1)`gem uninstall rake -v = 0.9.2` 2)`gem install rake -v = 0.9.2` (12认同)

djb*_*009 54

通过Railstutorial(demo_app)的第2章并遇到了这个问题.我尝试了这里列出的所有其他答案,但是直到我这样做才能让它工作:

把它放在你的Rakefile上面需要'rake':

require 'rake/dsl_definition'
Run Code Online (Sandbox Code Playgroud)

通过如何解决Heroku上未初始化的常量Rake :: DSL问题?

我还重新推荐并将所有文件推送到Github和Heroku.


Bra*_*tar 25

我需要做的就是使用:

gem install rake
Run Code Online (Sandbox Code Playgroud)

我已经有0.9.2版,只需要安装.


hoh*_*ner 19

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

gem uninstall rake -v=0.9.2 
gem install rake -v=0.9.2
Run Code Online (Sandbox Code Playgroud)

如果没有,请在Gemfile中指定版本"0.8.7".


Tra*_*der 10

如果不使用Bundler:

sudo gem install rake -v 0.8.7
sudo gem uninstall rake
Run Code Online (Sandbox Code Playgroud)

然后选择卸载0.9.0.


Jon*_*man 8

如果像我一样,你仍然坚持使用rake 0.8.7,并且你正在使用Rails 3.2.x,那么railties就增加了对Rake :: DSL的要求

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

module Rake
  module DSL
  end
end
Run Code Online (Sandbox Code Playgroud)


Gau*_*pta 7

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

在Gemfile中:

gem 'rake', '0.9.2'
Run Code Online (Sandbox Code Playgroud)

然后在控制台上运行:

sudo bundle update rake
Run Code Online (Sandbox Code Playgroud)

然后将以下行添加到Rakefile:

require 'rake/dsl_definition'
include Rake::DSL
Run Code Online (Sandbox Code Playgroud)


dat*_*yte 6

Rails 3.1.rc1已更新.对于您自己的Rakefiles,您可以在调用load_tasks之前添加它.

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

module ::RakeFileUtils
  extend Rake::FileUtilsExt
end
Run Code Online (Sandbox Code Playgroud)

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

更新:还注意到它已在这里得到解答:使用Rake 0.9.0的未定义方法'任务'