17 5 / 2011
How to render a full page template in a rake task with Rails 3
I stumbled upon this problem this week. There are many blog posts on how to do this in Rails 2, but not many for Rails 3 and most are broken in some ways. So I dug a little and found a nice solution.
First of all, Rails 3 is broken down into distinct modules that enables developer to pick and chose features and assemble them into something new. This is mostly what is happening with the new mailers. They include the features from a normal ActionController but in a different context. When you read the code of the mailers you will see that it includes a bunch of AbstractController modules. The same modules are also found in ActionController::Base. These are the basic building blocks to render templates in rails.
So this is how I created this OfflineTemplate class that allows you to render anything offline, in a rake task or in a cron job. You can render a partial, a string, XML, json or a page including its layout.
Comments on the implementation are welcome!