为什么我的自定义rake任务在lib/tasks中没有在Rails 3中发现?

时间:2022-05-31 22:27:59

Build-in rake tasks work fine, but my new custom one, in Project/lib/tasks/payments.rb doesn't get loaded:

在项目/lib/任务/支付中,内置的rake任务可以正常工作,但我的新定制任务是可以的。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'

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)

3 个解决方案

#1


161  

Change the file extension from .rb to .rake.

将文件扩展名从.rb更改为.rake。

#2


21  

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 a desc for my rake task, so make sure if you're writing your own task (i.e. not generating one) that you add a desc.

在这种情况下,没有.rake扩展会导致错误。然而,我今天在Rails 4.2应用程序上遇到了同样的问题,因为我的rake任务没有desc,所以请确保您正在编写自己的任务(即不生成任务),并添加desc。

For more information: http://guides.rubyonrails.org/command_line.html#custom-rake-tasks

更多信息:http://guides.rubyonrails.org/command_line.html # custom-rake-tasks

#3


0  

As per the Rails guide 2.10 Custom Rake Tasks

按照Rails指南2.10定制Rake任务

Custom rake tasks have a .rake extension and are placed in Rails.root/lib/tasks.

自定义rake任务有.rake扩展名,并被放在Rails.root/lib/tasks中。

But you have .rb extension.

但是你有。rb分机。

#1


161  

Change the file extension from .rb to .rake.

将文件扩展名从.rb更改为.rake。

#2


21  

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 a desc for my rake task, so make sure if you're writing your own task (i.e. not generating one) that you add a desc.

在这种情况下,没有.rake扩展会导致错误。然而,我今天在Rails 4.2应用程序上遇到了同样的问题,因为我的rake任务没有desc,所以请确保您正在编写自己的任务(即不生成任务),并添加desc。

For more information: http://guides.rubyonrails.org/command_line.html#custom-rake-tasks

更多信息:http://guides.rubyonrails.org/command_line.html # custom-rake-tasks

#3


0  

As per the Rails guide 2.10 Custom Rake Tasks

按照Rails指南2.10定制Rake任务

Custom rake tasks have a .rake extension and are placed in Rails.root/lib/tasks.

自定义rake任务有.rake扩展名,并被放在Rails.root/lib/tasks中。

But you have .rb extension.

但是你有。rb分机。