【问题标题】:Missing Template while running Cron Jobs运行 Cron 作业时缺少模板
【发布时间】:2014-03-19 15:42:59
【问题描述】:

当我们运行 cron 作业时,我们在 Rails 应用程序中遇到了一个奇怪的问题。我们的 cron 作业运行成功,但之后会导致“缺少模板”错误。除非我们解决这个问题,否则测试团队不会允许通过,所以请帮助!

提供代码sn-p和flow。

/etc/crontab

 55 5    * * *   user  sh /root_path/config/cron/shell_commands/environment/test.sh

test.sh

wget http://localhost:3000/cron_jobs/execute_cron_tasks/test/key

cron_jobs/execute_cron_tasks.rb

class CronJobs::ExecuteCronTasksController < ApplicationController
   def test
     CronTasks.test_cron if params[:key] && params[:key] == "key"
     respond_to do |format|
       format.html {render :status => Rack::Utils.status_code(:ok)}
       format.js {render :status => Rack::Utils.status_code(:ok)}
     end
   end
end

模块 cron_tasks.rb

module CronTasks
  def self.test_cron
    puts "=======================working===========================".red
  end
end

调用 cron 作业后,它会打印数据但抛出 500 Internal Server Error

服务器日志

Started GET "/cron_jobs/execute_cron_tasks/test/getmeinfirst" for 127.0.0.1 at 2014-02-19 15:27:13 +0530
Processing by CronJobs::ExecuteCronTasksController#test as */*
Parameters: {"key"=>"key"}
Completed 500 Internal Server Error in 30ms

ActionView::MissingTemplate (Missing template cron_jobs/execute_cron_tasks/test, application/test with {:locale=>[:en], :formats=>[:html], :handlers=>[:erb, :builder]}. Searched in:
* "/root_path/app/views"
* "/root_path/.rvm/gems/ruby-2.0.0-p353/gems/devise-1.4.2/app/views"
):

 app/controllers/cron_jobs/execute_cron_tasks_controller.rb:83:in `block (2 levels) in test'
 app/controllers/cron_jobs/execute_cron_tasks_controller.rb:82:in `test'


  Rendered /root_path/.rvm/gems/ruby-2.0.0-p353/gems/actionpack-3.2.13/lib/action_dispatch/middleware/templates/rescues/missing_template.erb within rescues/layout (0.3ms)

谢谢 问候

【问题讨论】:

    标签: ruby-on-rails ruby cron


    【解决方案1】:

    我假设您不想在正文中渲染任何内容。在这种情况下,请尝试

    format.html {render :status => Rack::Utils.status_code(:ok), :nothing => true}
    

    如果你确实想渲染一个视图,你可能应该把它放进去:

    app/views/cron_jobs/execute_cron_tasks/test.html.erb
    

    【讨论】:

    • 嗨,感谢重播。它现在工作正常,但有一个小问题。它正在创建一个名为“key”的文件,我将其与根文件夹中的请求一起传递。你能告诉我是什么问题吗?
    最近更新 更多