耙中止! NameError: uninitialized constant even add :environment to task

Posted

技术标签:

【中文标题】耙中止! NameError: uninitialized constant even add :environment to task【英文标题】:rake aborted! NameError: uninitialized constant even add :enviroment to task 【发布时间】:2016-11-23 18:44:33 【问题描述】:

我是 RoR 的新手,并尝试通过在 lib/电子表格下创建导入器来创建从谷歌电子表格导入数据库的任务。但是根据 SOF 中的其他一些问题,即使我添加了 :environment 任务,rake 也找不到我的进口商。这是我的文件

lib/电子表格/importer.rb

class SpreadSheet::Importer
  def initialize
    @session = GoogleDrive.saved_session("#Rails.root/config/google_drive/config.json")
  end

  def exec(table_name)
    #do something
  end
end

lib/tasks/spreadsheet.rake

namespace :spreadsheet do
  task :get => :environment do
    importer = Spreadsheet::Importer.new
    importer.exec
  end
end

错误:

rake 中止!

NameError: 未初始化常量SpreadSheet

/home/vagrant/workspace/ruby/kuwata-summer/lib/tasks/spreadsheet.rake:1:in <top (required)>

/home/vagrant/workspace/ruby/kuwata-summer/vendor/bundle/ruby/2.3.0 /gems/railties-5.0.0/lib/rails/engine.rb:654:in block in run_tasks_blocks

/home/vagrant/workspace/ruby/kuwata-summer/vendor/bundle/ruby/2.3.0/gems/railties-5.0.0/lib/rails/engine.rb:654:in each

/home/vagrant/workspace/ruby/kuwata-summer/vendor/bundle/ruby/2.3.0/gems/railties-5.0.0/lib/rails/engine.rb:654:in run_tasks_blocks

/home/vagrant/workspace/ruby/kuwata-summer/vendor/bundle/ruby/2.3.0/gems/railties-5.0.0/lib/rails/application.rb:443:in run_tasks_blocks

/home/vagrant/workspace/ruby/kuwata-summer/vendor/bundle/ruby/2.3.0/gems/railties-5.0.0/lib/rails/engine.rb:457:in load_tasks

/home/vagrant/workspace/ruby/kuwata-summer/rakefile:6:in <top (required)>

/home/vagrant/workspace/ruby/kuwata-summer/vendor/bundle/ruby/2.3.0/gems/rake-11.2.2/exe/rake:27:in <top (required)>

/home/vagrant/.rbenv/versions/2.3.1/bin/bundle:23:in load

/home/vagrant/.rbenv/versions/2.3.1/bin/bundle:23:in <main>

(通过使用 --trace 运行任务查看完整跟踪)

【问题讨论】:

【参考方案1】:

是的..你可以添加它自动加载,但另一种方法是通过“要求”在 rake 文件中添加

这是另一种方式,

lib/电子表格/importer.rb

module Spreadsheet
    class Importer
      def initialize
        @session = GoogleDrive.saved_session("#Rails.root/config/google_drive/config.json")
      end

      def exec(table_name)
        #do something
      end
    end
end

lib/tasks/spreadsheet.rake

您需要使用“reqire”来包含文件

namespace :spreadsheet do
  task :get => :environment do
    require "#Rails.root/lib/spreadsheet/importer"  
    importer = Spreadsheet::Importer.new
    importer.exec
  end
end

编码愉快!!!!!!

谢谢。

【讨论】:

【参考方案2】:

大写很重要。

Spreadsheet 已定义; SpreadSheet 不是。

这就是您的代码在尝试定义 SpreadSheet::Importer 时抛出错误的原因。

...或者至少,根据我所掌握的有限信息,它看起来像。如果Speadsheet 没有在您的代码中的其他任何地方实际定义,那么您可以改为:

module Spreadsheet # Can be a class, and can instead be SpreadSheet - as long as you're consistent!!
  class Import
    # ...
  end
end

【讨论】:

抱歉输入错误,但现在可以通过将自动加载添加到 config/application.rb config.autoload_paths += %W(#config.root/lib)

以上是关于耙中止! NameError: uninitialized constant even add :environment to task的主要内容,如果未能解决你的问题,请参考以下文章

耙中止! TypeError: Parts 不是一个类

耙中止! :name:Symbol 的未定义方法“map”

Ruby on Rails。捆绑器。黄瓜。耙中止!命令失败,状态为 (1)

Ubuntu rake 中止! NameError:未初始化的常量 ActionDispatch::XmlParamsParser

耙子中止未定义的方法“每个”

ruby 耙任务示例#rake