Everyday Rails

Bootstrapping Rails with an app template

By Aaron Sumner, 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.

Discussion

Follow along on on Mastodon, Facebook, or Bluesky to keep up-to-date with my latest posts. Better yet, subscribe to my newsletter for updates from Everyday Rails, book picks, and other thoughts and ideas that didn't quite fit here.
Buy Me A Coffee

Test with confidence!

If you liked my series on practical advice for adding reliable tests to your Rails apps, check out the expanded ebook version. Lots of additional, exclusive content and a complete sample Rails application.

Newsletter

Ruby on Rails news and tips, and other ideas and surprises from Aaron at Everyday Rails. Delivered to your inbox on no particular set schedule.