Bootstrapping Rails with an app template

May 22, 2010

In my first post I walked through the steps I take when creating a Rails application from scratch. I should clarify that those are the steps I would take if it weren’t for app templates, a very useful feature introduced in Rails 2.3. Simply put, create a Ruby file like the following and put it somewhere you can access it–in this example I’ll just keep it in my Home folder on my Mac, and call it rails_template.rb:

# rails_template.rb

# set up the databases
rake "db:create", :env => 'development'
rake "db:create", :env => 'test'

# install required gems
gem "haml"
rake "gems:install"
run "haml --rails ."

# run nifty generators
generate :nifty_layout, "--haml"
generate :nifty_config

# remove unneeded files from public directory
run "rm public/index.html"
run "rm public/images/rails.png"

# set up git
file ".gitignore", <<-END
.DS_Store
log/*.log
tmp/**/*
config/database.yml
db/*.sqlite3
END

run "touch tmp/.gitignore log/.gitignore vendor/.gitignore"
run "cp config/database.yml config/database.example"

git :init
git :add => "."
git :commit => "-a -m 'create initial application'"

Now, to use it, create your new Rails app with the following command:

$ rails -d mysql mygreatapp -m ~/rails_template.rb

and watch the magic happen.

This is a very basic app template, but given their recipe-like nature, lots of templates are available on GitHub and elsewhere to help get your Rails applications off the ground. In particular, check out Mike Gunderloy’s BigOldRailsTemplate, which includes an authentication system, pagination, database options (instead of my MySQL-only example above), testing, and other services.

My advice is to start with a basic app template, then expand it as you build a collection of go-to gems and plugins. A great place to get started is by checking out the Railscasts episode on app templates, as well as Ryan Bates’ repository of sample templates on GitHub.

Rails testing made simple

Learn to test Rails apps the way I learned, building up tests step-by-step, in Everyday Rails Testing with RSpec. Expanded to include exclusive content and a complete sample Rails application. Learn more »

Also available on Amazon.com.

blog comments powered by Disqus