我在 Rails 3.2 应用程序中使用 gem 'clockwork'。该应用程序在 Heroku 上运行。时钟作业在凌晨 1 点运行 - 但它不执行代码。
这是 clock.rb 代码:
Clockwork.every(1.day, 'DailyJob', :at => '01:00'){
StatsMailer.stats_email.deliver
Forecast.where(:run_date => Date.today).each do |forecast|
woschedules_run_jobplans_path(:id => forecast.woschedule_id)
end
}
日志显示:
Sep 04 00:00:05 ndeavor-staging app/clock.1: #<NoMethodError: undefined method `woschedules_run_jobplans_path' for Clockwork:Module>
如果我收集路线,我会得到:
woschedules_run_jobplans GET /woschedules/run_jobplans(.:format) woschedules#run_jobplans
最佳答案
错误是路由不存在。由于路由确实存在,这个问题通常意味着 Rails 路由尚未加载到当前范围。
您可能需要包含 Rails 的路由助手以使其正常运行:Rails.application.routes.url_helpers
Clockwork.every(1.day, 'DailyJob', :at => '01:00'){
StatsMailer.stats_email.deliver
Forecast.where(:run_date => Date.today).each do |forecast|
# Prepend your route with the following:
Rails.application.routes.url_helpers.woschedules_run_jobplans_path(:id => forecast.woschedule_id)
end
}
或者,为了稍微干一点,您可以简单地在文件的开头包含所有的路由助手,使用:
# Beginning of file
include Rails.application.routes.url_helpers
# ...
关于ruby-on-rails - Rails 发条不运行代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32399060/