2

I need to be able to capture the output of a controller action as a string - you'd think it would be a relatively easy thing but I can't get any of the render/render_to_string methods to work.

I need to be able to store a reference to a controller and action and then be able to grab it's output further on in a process (actually server side printing)

Any ideas - thanks?

Using render/render_to string like (in a model)

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

this results in;

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

if i try to use render_to_string I get;

undefined method `render_to_string' for #<ActionView::Base:0x0000010400c760> (NoMethodError)
apneadiving
  • 114,565
  • 26
  • 219
  • 213
John Beynon
  • 37,398
  • 8
  • 88
  • 97
  • show the way you use `render_to_string` – fl00r Aug 15 '11 at 10:55
  • possible duplicate of [How do I get the rendered output of a controller's action without visiting the web page?](http://stackoverflow.com/questions/4106724/how-do-i-get-the-rendered-output-of-a-controllers-action-without-visiting-the-we) – Simon Aug 15 '11 at 13:20

1 Answers1

1

Why don't you use curl?

curl http://localhost:3000/controller/action

Using curl gem, you can call it even from console:

require 'curl'
page = CURL.new.get('http://localhost:3000/controller/action')

UPDATE

From the rails console:

app.get('/controller/action')
app.response.body
Lukas Stejskal
  • 2,542
  • 19
  • 30