Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/54.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/40.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 捕获控制器动作的输出_Ruby On Rails - Fatal编程技术网

Ruby on rails 捕获控制器动作的输出

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:

我需要能够将控制器操作的输出捕获为字符串—您可能认为这是一件相对简单的事情,但我无法让任何render/render\u to\u字符串方法正常工作

我需要能够存储对控制器和操作的引用,然后能够在进程中进一步获取它的输出(实际上是服务器端打印)

有什么想法吗-谢谢

使用render/render_进行字符串化(在模型中)

如果我尝试使用render_to_字符串,我会得到

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