在 rails 中,我构建了一种 cronjob,但是一旦 View 在那里呈现,我想在处理的任务上像“signoff”一样运行一些代码。
我应该把代码放在哪里,以便它在处理的绝对结束时运行(一旦呈现 View )
rails 如何处理 html 缓冲? (它会在渲染时或渲染后将 HTML 刷新给用户吗?)
最佳答案
您可以将 View 渲染为字符串,捕获渲染时可能发生的任何超时,记录操作结果,并返回字符串:
begin
@elements = Element.find(:all)
html = render_to_string
# Store the result
Result.create(:element_count => @elements.count)
rescue Timeout::Error
# Store the result of the call as failed?
Result.create(:element_count => 0)
end
send_data html, :disposition => 'inline', :type => 'text/html'
您可以做一些其他的事情来实现您的目标:
如果您需要执行的代码仅由您的 cron 作业触发,您可以使用 rake 任务而不是 Controller 操作。
您可以调用一个脚本来向您的 Controller 发出请求,检查请求的输出,然后记录结果(可能通过调用 Controller 中的新操作)。
关于html - 在 Rails 中呈现 View 后评估代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9802141/