Ruby on rails 捕获控制器动作的输出
我需要能够将控制器操作的输出捕获为字符串—您可能认为这是一件相对简单的事情,但我无法让任何render/render\u to\u字符串方法正常工作 我需要能够存储对控制器和操作的引用,然后能够在进程中进一步获取它的输出(实际上是服务器端打印) 有什么想法吗-谢谢 使用render/render_进行字符串化(在模型中) 如果我尝试使用render_to_字符串,我会得到Ruby on rails 捕获控制器动作的输出,ruby-on-rails,Ruby On Rails,我需要能够将控制器操作的输出捕获为字符串—您可能认为这是一件相对简单的事情,但我无法让任何render/render\u to\u字符串方法正常工作 我需要能够存储对控制器和操作的引用,然后能够在进程中进一步获取它的输出(实际上是服务器端打印) 有什么想法吗-谢谢 使用render/render_进行字符串化(在模型中) 如果我尝试使用render_to_字符串,我会得到 undefined method `render_to_string' for #<ActionView::Base:
undefined method `render_to_string' for #<ActionView::Base:0x0000010400c760> (NoMethodError)
用于#的未定义方法“render_to_string”(NoMethodError)
为什么不使用curl
curl http://localhost:3000/controller/action
使用curl gem,您甚至可以从控制台调用它:
require 'curl'
page = CURL.new.get('http://localhost:3000/controller/action')
更新
从rails控制台:
app.get('/controller/action')
app.response.body
显示您使用
render_to_string
的方式,这可能是一个选项的可能副本-URL受保护,但必须通过本地连接在本地克服此问题确定,请参阅我的更新。有几种方法可以直接从rails控制台获取页面内容,这是最简单的方法。
require 'curl'
page = CURL.new.get('http://localhost:3000/controller/action')
app.get('/controller/action')
app.response.body