layout | title | excerpt |
---|---|---|
post |
Bootstrapping Rails with an app template |
Now that you've seen how I configure my Rails applications out of the gate, here's a way to do the same thing with one command. |
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
:
{% highlight ruby %}
rake "db:create", :env => 'development' rake "db:create", :env => 'test'
gem "haml" rake "gems:install" run "haml --rails ."
generate :nifty_layout, "--haml" generate :nifty_config
run "rm public/index.html" run "rm public/images/rails.png"
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'" {% endhighlight %}
Now, to use it, create your new Rails app with the following command:
{% highlight bash %} $ rails -d mysql mygreatapp -m ~/rails_template.rb {% endhighlight %}
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.