October 07, 2010

#9 Deployment with Heroku

Here we will add a formal home page and go live with our app. We'll also setup our domain name.

Adding a Pages Controller

We'll setup a controller for static pages since our home page won't be doing changing much at this point.
class PagesController < ApplicationController
  skip_filter :authorize    # added 1/9/11

  def home

The Home Page View

You've already seen it.
- title "Building a Ruby on Rails Website"

  %h2 About aRailsDemo
    This ...
  %h2 About the Author
  %p Programming ...

Adding the Route

We register the new route and change the old root.
Arailsdemo::Application.routes.draw do
  get "pages/home"
  root :to => "pages#home"

A Couple of Setting Changes Before Deployment

Heroku is a read only service. If you need write access, you can use Amazon S3. One exception is the temp folder. I say this because the way Sass and Compass works is that the .sass and .scss files are complied into .css files that our application uses. If it tries to do this on Heroku, you'll get an error message. (Although, after reloading you will be able to see the site.)

There is a gem (Hassle) that takes advantage of the write access on the temp directory. I chose not to use this gem because when I view the site in development mode, the .css files get generated in that mode. So I figure there's really no need for Sass/Compass to compile in production mode. So we make that configuration change. Also, we will tell Compass not to put comments into the generated files.

It used to be that Heroku required you to add the pg gem (for PostgreSQL), however, I did not have to do that this time.
config/compass.rb and config/environments/production.rb
# compass.rb
line_comments = false

# production.rb
Arailsdemo::Application.configure do
  Sass::Plugin.options[:never_update] = true

Putting Up Our Site on Heroku

After a couple of days of making the base application, I was itching to throw it into the interwebs. I'm not going to go over setting up an Heroku account here. (I had done that a while back, and I don't remember the details. But I think it was pretty straight forward.)

I haven't been showing it, but we've been frequently committing to our git repository. So all we have to do is create our Heroku app, upload it, and migrate the database. With this, we can access our app at 'www.arailsdemo.heroku.com'.
> heroku create arailsdemo
> git push heroku master
> heroku rake db:migrate

Getting a Domain Name

I've never done this before, so I spent a good part of today figuring it out. GoDaddy was of course the obvious choice. However, they charged a fee for their privacy service. Somehow I decided on Namecheap.com. The setup process was pretty straight forward.

With a domain name, I had to update the Heroku app. First, I had to add the custom domains addon. Then I could tell it my new domain name. Finally, I went back to the Namecheap website and configured the setting so that my new domain name was an alias that points to 'proxy.heroku.com' (i.e. CNAME).
> heroku addons:add custom_domains
> heroku domains:add www.arailsdemo.com
> heroku domains:add arailsdemo.com

Redirecting arailsdemo.heroku.com

In order make sure only our new domain name is shown in the address bar of our browser, we have to have our app do some rerouting. The Heroku website gives us some code for this. I modified it so that the redirection does not happen in development mode.
config/initializers/constants.rb and app/controllers/application_controller.rb
# constants.rb
APP_ADDRESS = 'www.arailsdemo.com'

# application_controller.rb

class ApplicationController < ActionController::Base
  before_filter :ensure_domain, :authorize    # edit 1/9/11
  def ensure_domain
    if request.env['HTTP_HOST'] != APP_ADDRESS && ENV["RAILS_ENV"] != 'development'
      redirect_to("http://" + AP_ADDRESS)


(Please login to submit comments.)

View All Posts