Ruby-on-rails 简单的 rails rake 任务拒绝运行并出现错误“不知道如何构建任务”,为什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12903069/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 20:52:12  来源:igfitidea点击:

Simple rails rake task refuse to run with error "Don't know how to build task", why?

ruby-on-railsrakerake-task

提问by Rubytastic

I have this simple rake task which refuses to run. I just don't see why it looks correct. Who can pinpoint me to the probably very simple mistake I made? Thank you!

我有一个拒绝运行的简单 rake 任务。我只是不明白为什么它看起来是正确的。谁能指出我犯的可能非常简单的错误?谢谢!

/lib/tasks/reindex.rb:

/lib/tasks/reindex.rb:

namespace :db do

  desc "Tire reindex profiles"

  task :reindex => :environment do
    system "cd #{Rails.root} && rake environment tire:import CLASS='Profile' FORCE=true"
  end

end

The error:

错误:

rake db:reindex
rake aborted!
Don't know how to build task 'db:reindex'

回答by cjc343

Rename your file to reindex.rakeand it should work.

将您的文件重命名为reindex.rake,它应该可以工作。

Related: How to build task 'db:populate'

相关:如何构建任务“db:populate”

回答by esc_rtn

You can also get this error if you forget to put the namespace before your task name. (i.e. :reindex instead of db:reindex)

如果您忘记将命名空间放在任务名称之前,您也会收到此错误。(即 :reindex 而不是 db:reindex)

回答by David Hempy

The file extension for rake tasks must be '.rake'.

rake 任务的文件扩展名必须是“.rake”。

If you named your file as '.rb', then rake will not find it, and you will question your own sanity for several minutes before ending up here.

如果您将文件命名为“.rb”,那么 rake 将找不到它,并且您将在几分钟内质疑自己的理智,然后才到此为止。

回答by Morgan

Don't forget to check that you call the name of the task and not the file name. The best thing is that they be named the same.

不要忘记检查您是否调用了任务的名称而不是文件名。最好的事情是它们的名字相同。

回答by super1ha1

This error happen to me is because the namespace name got underscore

这个错误发生在我身上是因为命名空间名称有下划线

As is: deploy_app  (not work)
To be: deployapp   (working)