26 November 2010

Install Rails 3 on OS X with Devise, RSpec, HAML, and jQuery Support

Goals

  • PostgreSQL - Instead of MySQL
  • Devise - Instead of AuthLogic or Restful Authentication
  • HAML - Instead of ERB
  • jQuery - Instead of Prototype
  • RSpec - Instead of Test-Unit

Prerequisites

  • Ruby 1.9.2 - Installed via Homebrew. These instructions not tested with RVM.
  • Rails 3.0.3 - gem install rails
  • PostgreSQL - See my notes on installing PostgreSQL 9 on OS X via Homebrew.

Create App

rails new myapp
cd myapp

Create Database

Change config/database.yml accordingly to point to your dev DB, then:

rake db:create

Gems

Add to Gemfile in the root of your app:

gem 'autotest'
gem 'devise'
gem 'haml-rails' # Not haml
gem 'hpricot'
gem 'pg'
group :development, :test do
  gem 'rspec-rails', '~> 2.1'
end
gem 'ruby_parser'

Run sudo bundle install to install all gems. Note haml-rails instead of haml. haml-rails is newer and includes Rails 3 generators. Also, hpricot and ruby_parser are necessary or Devise's generators fail.

Devise

Follow the Devise instructions at the end. There are a couple of manual steps. If you missed it, those steps are:

Add to config/environments/development.rb:

config.action_mailer.default_url_options = { :host => 'localhost:3000' }

Add to config/environments/production.rb:

config.action_mailer.default_url_options = { :host => 'russbrooks.com:3000' }

Add to config/routes.rb:

root :to => "home#index"

Since we are going to use HAML templates, rename app/views/layouts/application.html.erb to application.html.haml, then paste:

!!!
%html{ :lang => 'en', 'xml:lang' => 'en', :xmlns => 'http://www.w3.org/1999/xhtml' }
  %head
    %title
      My New Site
    = stylesheet_link_tag :all
    = javascript_include_tag :defaults
    = csrf_meta_tag
  %body{ :class => @body_class }
    #wrap
      #header
      #body
        %p{ :class => :notice }
          = notice
        %p{ :class => :alert }
          = alert
        = yield
      #footer

Remove Rails' welcome page or it will render instead of home#index:

rm public/index.html

Use HAML Instead of ERB

Add this to config/application.rb anywhere inside the Application class:

# Default template engine for Rails Generators. Use HAML instead of ERB.
config.generators do |g|
  g.template_engine :haml
end

The generators are now built in to the HAML-Rails gem. There is no need to add the rails3-generators gem.

Use RSpec Instead of Test-Unit

We already added the necessary gems to Gemfile and did a bundle install above, so just run the RSpec installation generator:

rails g rspec:install

This makes Rails generators produce RSpec tests instead of Test-Unit tests.

Use jQuery Instead of Prototype

rake rails:template LOCATION=http://github.com/lleger/Rails-3-jQuery/raw/master/jquery.rb

Generate User Model and DB Table

rails g devise User
rake db:migrate

Use rake routes to see the routes Devise created for sign_up, sign_in, sign_out, etc. You should be able to access those in a browser to test them at this point. See the Devise docs for more info.

From there, you might want to start with some scaffolding to ensure everything worked right, for example, that it generates HAML, RSpec tests, and is using jQuery. For example, maybe create a "Post" resource if you are creating a blog:

rails generate scaffold Post title:string teaser:string body:text

Filed under Mac

Archives