[英]Run a custom rake task on rails
我正在嘗試執行自定義的rake
任務,但似乎無法正常運行。 我的代碼如下所示:
namespace :demotask do
desc "display the current environment of rake"
task :current_environment => :environment do
puts "You are running rake task in #{Rails.env} environment"
end
end
並放置在lib/tasks
下名為test.rake
的文件中。 Rakefile
在那里,但是在控制台中運行任務時確實出現此錯誤:
NameError: undefined local variable or method 'current_environment' for main:Object
我也嘗試重新啟動服務器。
您應該使用namespace
從終端調用它,使用bundler
可能是一個好主意:
bundle exec rake demotask:current_environment
另外,嘗試將任務定義更改為:
task :current_environment => :environment do
...
包括=> :environment
將告訴Rake加載整個應用程序環境。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.