捕获控制器动作的输出

时间:2011-08-15 10:41:32

标签: ruby-on-rails

我需要能够将控制器操作的输出捕获为字符串 - 您认为这将是一个相对容易的事情,但我无法使任何render / render_to_string方法起作用。

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

任何想法 - 谢谢?

使用render / render_to字符串(在模型中)

view = ActionView::Base.new(ActionController::Base.view_paths)
view.extend ApplicationHelper
output = view.render(:action => '<someaction>', :controller => '<somecontroller')

这会导致;

/Users/---/gems/activesupport-3.0.5/lib/active_support/whiny_nil.rb:48:in `method_missing': undefined method `formats' for nil:NilClass (NoMethodError)

如果我尝试使用render_to_string,我会得到;

undefined method `render_to_string' for #<ActionView::Base:0x0000010400c760> (NoMethodError)

1 个答案:

答案 0 :(得分:1)

你为什么不使用卷发?

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
相关问题