如何在不访问网页的情况下获取控制器操作的渲染输出? [英] How do I get the rendered output of a controller's action without visiting the web page?

查看:26
本文介绍了如何在不访问网页的情况下获取控制器操作的渲染输出?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

作为 rails 2.3 应用程序部署过程的一部分,我想将错误页面的静态版本保存到公共文件夹中.如何在不访问网页的情况下获取控制器操作的渲染输出?我知道这是可以做到的,因为功能测试做到了 - 如果我说

As part of the deployment process for our rails 2.3 app, I'd like to save static versions of our error pages to the public folder. How do I get the rendered output of a controller's action without visiting the web page? I know it can be done because the functional tests do it - if I say

get :errors, :id => 404

然后正文在@response.body 中.我想我可以从 ActionController::TestCase 中复制代码,但我希望有一种更简单的方法来做到这一点.

then the body is in @response.body. I suppose I could just copy the code out of ActionController::TestCase, but I'm hoping that there's a simpler way to do it.

推荐答案

最后我确实进入了 ActionController::TestCase,这是我挖出来的:

In the end I did just go into ActionController::TestCase, and this is what I dug out:

def get_content host, path, filename
  request = ActionController::Request.new 'HTTP_HOST' => host,
                                          'REQUEST_URI' => path,
                                          'REQUEST_METHOD' => 'GET',
                                          'rack.input' => '',
                                          'rack.url_scheme' => 'http'

  controller = ActionController::Routing::Routes.recognize(request).new
  response = ActionController::Response.new
  controller.process request, response
  return response.body
end

这篇关于如何在不访问网页的情况下获取控制器操作的渲染输出?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆