Rails发条没有运行代码

Red*_*irt 7 ruby ruby-on-rails heroku ruby-on-rails-3.2 clockwork

我在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
  }
Run Code Online (Sandbox Code Playgroud)

日志显示:

9月04日00:00:05 ndeavor-staging app/clock.1:#<NoMethodError:未定义方法`woschedules_run_jobplans_path'用于发条:模块>

如果我耙路线,我得到:

woschedules_run_jobplans GET   /woschedules/run_jobplans(.:format) woschedules#run_jobplans
Run Code Online (Sandbox Code Playgroud)

Wes*_*ter 7

错误在于路线不存在.由于路径存在,这个问题通常意味着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
}
Run Code Online (Sandbox Code Playgroud)

或者,要稍微干一点,您可以使用以下命令在文件开头简单地包含所有路径助手:

# Beginning of file
include Rails.application.routes.url_helpers

# ...
Run Code Online (Sandbox Code Playgroud)