Reputation: 37507
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)
Upvotes: 2
Views: 1564
Reputation: 2562
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
Upvotes: 1