Sevenforge Sevenforge by Curtis Spencer home

Rendering rhtml files to string without a controller

October 29, 2008

Sometimes you need to pierce the MVC veil and render a view somewhere lower level without an HTTP request, such as preemptively refreshing a cache without a client request, or outputing rhtml to a static html file. To preserve some of the “Don’t repeat yourself mantra”, it would be nice to have a way to render an .rhtml without a request/response.

av = ActionView::Base.new(Rails::Configuration.new.view_path)
av.assigns[:item] = @item
output = av.render('controller/view')

Thanks to these blog posts: Choonkeat and acts_as_renderer for the inspiration.


Fork me on GitHub