Ben Scofield

me. still on a blog.

Application Templates in Rails 3

Here’s a trick for creating modular templates in Rails 3: use the apply method Say you like to use Rails templates, but you find them a little too inflexible – you want to use Cucumber, jQuery, and MongoDB, but not always together. You could create separate templates for each combination (and in fact, with Rails 2.3.x that’s what you have to do), but in Rails 3, things get a little easier. With 3, you can actually modularize your templates! For instance, this gist removes the default Prototype JavaScript files from your app, and gives you a simple XHTML layout with Google’s hosted versions of jQuery all ready to go: jQuery application template This gist, on the other hand, requires the Cucumber and webrat gems and runs the Cucumber generator to set up the features directory, etc: Cucumber application template In current Rails, you’d either have to run these individually or put the code into a single file to get them to apply to a project; in Rails 3, however, you can create a separate template that applies both of them: Aggregate template In Rails 3, the directive to apply a template has been refactored into the apply command, which you can access inside a template, because they’re instance_eval‘d by the application generator. This opens up a whole new world of possibilities for remixing components, and should make starting new Rails applications even easier. One more reason to anticipate the arrival of 3!