Enumerable Blog
About coding, web development, and fun to be had in tech

Setting Up a New Rails Application

Last Updated:

Update: More Rails Setup Guides

After writing this post, I found the official Rails Setup Tutorials on Github. These will likely stay up to date and do include more setup variants. Enjoy!

Recently I started working on a new Rails application. It's fun to write code for a new application but there's a not so little step called setup that needs to happen first. So, today, I'd like to share a recipe that I use when starting a new Rails application. I have found that having my recipe saved in a text file is a huge time saver. Something that could be even more time saving if you don't have too many customizations between apps is to have a template app that you can copy over to a new folder :).

There are a lot of default settings in Rails which is great (Convention over Configuration!) but I have a number of settings that I like to personalize. I change settings to use: rSpec rather than TestUnit, Heroku with a PostgreSQL database, HAML and SASS, HTML5 Boilerplate and Compass, and the list goes on.

My Ruby on Rails Recipe

Steps to setup a Rails App:

> rails new AppName -T  #Skips TestUnit (in favor of rspec), specify database with "-d postgresql"
> cd AppName
> git init/add/commit 'fresh'

# Add to Gemfile under assets group:
gem 'therubyracer'
# These are in assets so that when you use generators (in development) the proper files will be created.
gem 'factory_girl_rails'
gem 'rspec-rails'
gem 'cucumber-rails'

# Add outside groups:
gem 'haml-rails'

> bundle install
> git add/commit 'gems installed'

# config/application.rb
module AppName
  class Application < Rails::Application
    config.generators do |g|
      g.template_engine :haml
    end
  end
end

# And in a similar block inside config/environments/test.rb
  g.test_framework :rspec, :views => false, :fixture => true
  g.fixture_replacement :factory_girl, :dir => 'spec/factories'

> git add/commit 'generators configured'

# Edit config/database.yml
# http://guides.rubyonrails.org/getting_started.html#configuring-a-database
> git add/commit 'setup databases'

# Remove unwanted assets
> git rm ___
# app/assets/images/rails.png, public/index.html, replace public/favicon.ico, etc
> git commit

# Inside /app/views/layouts/
> html2haml application.html.erb application.html.haml
> git rm application.html.erb
# Customize the application layout in app/views/layouts/
# Create a Controller method for the homepage in app/controllers/application_controller.rb (def root)
# Create the homepage route in config/routes.rb: root :to => 'application#root'
> git add/commit

# Add base, generic, modules folders to app/assets/stylesheets/

> git mv application.css application.css.scss

# Delete comments at top of application.scss and add to it:
@import "base/*";
@import "generic/*";
@import "modules/*";
@import "helpers/*";

# Want HTML5 Boiler Plate?
#  Add header CSS in base/h5bp.scss and footer css in helpers/h5bp.scss
> git add/commit

# Want the Compass CSS Framework?
#  Add `@import "compass";` and `@import "compass/reset";` to application.scss before the other imports
#  Add 'compass-rails' to Gemfile under :assets
> bundle
> bundle exec compass init rails
> mkdir app/assets/stylesheets/compass
> git mv app/assets/stylesheets/*.css.scss app/assets/stylesheets/compass/
> git commit -m 'set up scss architecture with compass'

# Add some empty scss files (like style.scss, reset.scss, etc) to folders
#  that don't have any CSS files to avoid compile errors
> git add/commit

> rails g rspec:install
> rails g cucumber:install

# This is probably unnecessary if you're using factory_girl_rails rather than factory_girl
# Add this to spec/spec_helper.rb under other requires
require 'factory_girl'
FactoryGirl.find_definitions
> git add/commit

# Customize CSS frameworks to play nicely with one another... ech
> git add/commit

# Get Heroku set up
# Install heroku if you don't have the command line utility: http://toolbelt.heroku.com/
> heroku update
> heroku login
> heroku apps:create myapp
> git add/commit (if needed)
> git push heroku master

# Start setting up your app with scaffolds
> rails generate scaffold Post release:date
> rake db:migrate
> rake db:test:prepare

> git add/commit 'Scaffolding Generated'

I hope this is helpful. Let me know if you have any questions or suggestions in the comments :)

Check out the next post covering the single responsibility principle and object oriented design. It was inspired by talks given by Sandi Metz and Ben Orenstein at Rocky Mountain Ruby Conference 2012.

JOIN THE DISCUSSION
comments powered by Disqus