为什么在 Rails 3 中没有发现 lib/tasks 中我的自定义 rake 任务?
内置的 rake 任务工作正常,但我的新自定义任务(在 Project/lib/tasks/ payments.rb 中)未加载:
namespace :payments do
desc "Tally payments at the end of the month"
task :compute => :environment do
BillingPeriod.compute_new_period
end
end
$ rake payments:compute
(in /Users/rob/Code/Apps/skyfarm)
rake aborted!
Don't know how to build task 'payments:compute'
如果我加载文件 application.rb,它工作正常:
require 'lib/tasks/payments.rb'
...但它会破坏其他东西:
$ rails s
./lib/tasks/payments.rb:1: undefined method `namespace' for main:Object (NoMethodError)
Build-in rake tasks work fine, but my new custom one, in Project/lib/tasks/payments.rb doesn't get loaded:
namespace :payments do
desc "Tally payments at the end of the month"
task :compute => :environment do
BillingPeriod.compute_new_period
end
end
$ rake payments:compute
(in /Users/rob/Code/Apps/skyfarm)
rake aborted!
Don't know how to build task 'payments:compute'
It works fine if I load the file application.rb:
require 'lib/tasks/payments.rb'
...but it breaks other things:
$ rails s
./lib/tasks/payments.rb:1: undefined method `namespace' for main:Object (NoMethodError)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
将文件扩展名从
.rb
更改为.rake
。Change the file extension from
.rb
to.rake
.在这种特定情况下,没有
.rake
扩展名导致了错误。然而,我今天在 Rails 4.2 应用程序中遇到了同样的问题,这是因为我没有用于我的 rake 任务的desc
,所以请确保您是否正在编写自己的任务(即不是生成一个),您添加一个desc
。有关更多信息:http://guides.rubyonrails.org/command_line.html#custom -rake-任务
In this specific case, not having a
.rake
extension caused the error. However, I had the same issue with a Rails 4.2 app today, and it was because I did not have adesc
for my rake task, so make sure if you're writing your own task (i.e. not generating one) that you add adesc
.For more information: http://guides.rubyonrails.org/command_line.html#custom-rake-tasks
根据 Rails 指南 2.10 自定义 Rake 任务
但你有
.rb
扩展名。As per the Rails guide 2.10 Custom Rake Tasks
But you have
.rb
extension.