Rails 3 - possible to render a view into a string (inside a model or controller)?

In Rails 2 I could do this to get the view as a string (i.e. to save it to a file):

av = ActionView::Base.new(Rails::Configuration.new.view_path)
html = av.render(
    :partial => "comparisons/display",
    :locals => { :comparison => self, :format_html => nil, :print_view => print_view, :fully_qualified_url_prefix => fully_qualified_url_prefix, :user => user }
)

In Rails 3 it seemingly does not work anymore. In reference to https://rails.lighthouseapp.com/projects/8994/tickets/3471-actionviewassigns-lost-its-usefulness, Yehuda Katz’s comment (in reference to passing necessary parameters to ActionView::Base.new) is “This was modified in order to improve performance and reduce the number of instance variable assigns we need to do in cases with many renders inside a single view.”

So… is there a way to do this anyone knows of in Rails 3? Even if I can do so inside a controller I guess that would also be ok (I will try this next), but would prefer to leave things as they are.

Thanks,

David

David Kahn wrote in post #957895:

In Rails 2 I could do this to get the view as a string (i.e. to save it to a file):

    av = ActionView::Base.new(Rails::Configuration.new.view_path)     html = av.render(         :partial => "comparisons/display",         :locals => { :comparison => self, :format_html => nil, :print_view => print_view, :fully_qualified_url_prefix => fully_qualified_url_prefix, :user => user }     )

In Rails 3 it seemingly does not work anymore.

Did you perchance want render_to_string (which also exists in Rails 2)?

Best,

David Kahn wrote in post #957895:

In Rails 2 I could do this to get the view as a string (i.e. to save it

to a

file):

av = ActionView::Base.new(Rails::Configuration.new.view_path)
html = av.render(
    :partial => "comparisons/display",
    :locals => { :comparison => self, :format_html => nil,

:print_view

=> print_view, :fully_qualified_url_prefix =>

fully_qualified_url_prefix,

:user => user }

)

In Rails 3 it seemingly does not work anymore.

Did you perchance want render_to_string (which also exists in Rails 2)?

Hmmm… yup, that works, thanks. Would rather do that in the model but I can pass it in and step aside from this mess. I must have been hankering for punishment in doing it the way I did it in the first place.

David Kahn wrote in post #957895:

In Rails 2 I could do this to get the view as a string (i.e. to save it

to a

file):

av = ActionView::Base.new(Rails::Configuration.new.view_path)
html = av.render(
    :partial => "comparisons/display",
    :locals => { :comparison => self, :format_html => nil,

:print_view

=> print_view, :fully_qualified_url_prefix =>

fully_qualified_url_prefix,

:user => user }

)

In Rails 3 it seemingly does not work anymore.

Did you perchance want render_to_string (which also exists in Rails 2)?

Hmmm… yup, that works, thanks. Would rather do that in the model but I can pass it in and step aside from this mess. I must have been hankering for punishment in doing it the way I did it in the first place.

Actually just discovered the reason I tried to do this in the model: I need to render this view to string from other controllers/models. Do you know offhand if I can just instantiate this controller and still call one of it’s methods to string from a different controller or even better from a model? Just trying it right now and getting “NameError Exception: uninitialized constant InstallerController::ComparisonController” (foreign controller name is InstallerController).

David Kahn wrote in post #957906:

> av = ActionView::Base.new(Rails::Configuration.new.view_path)

Did you perchance want render_to_string (which also exists in Rails 2)?

Hmmm... yup, that works, thanks. Would rather do that in the model but I can pass it in and step aside from this mess. I must have been hankering for punishment in doing it the way I did it in the first place.

Actually just discovered the reason I tried to do this in the model: I need to render this view to string from other controllers/models.

You should never be rendering anything from a model. Any rendering -- at all -- is the responsibility of the controller and view.

Do you know offhand if I can just instantiate this controller and still call one of it's methods to string from a different controller or even better from a model?

render_to_string :controller => 'some_other_controller', :action => 'export_file'

That should be all you need, just like plain old render. Does that not work?

Best,

David Kahn wrote in post #957906:

Do you know

offhand if I can just instantiate this controller and still call one of

it’s

methods to string from a different controller or even better from a

model?

render_to_string :controller => ‘some_other_controller’, :action =>

‘export_file’

That should be all you need, just like plain old render. Does that not

work?

Ok, I am loosing my mind, yes, this works beautifully! I did not make the connection between render_to_string and the normal render action being of the same abilities.