Skip to content

Instantly share code, notes, and snippets.

@jaymcgavren
Created May 25, 2012 05:47
Show Gist options
  • Star 20 You must be signed in to star a gist
  • Fork 8 You must be signed in to fork a gist
  • Save jaymcgavren/2786016 to your computer and use it in GitHub Desktop.
Save jaymcgavren/2786016 to your computer and use it in GitHub Desktop.
A Code TV screencast on getting started with Heroku

Description

Heroku is a simple way to publish your Rails app, and a powerful platform that will allow it to scale. In this episode, Jay McGavren gets you started with your first Heroku app.

Set up your Rails app

Isolate your gem environment

  • You WANT Rails to fail locally if a gem isn't in your Gemfile

  • Otherwise you're in for a surprise when it's missing on Heroku

    $ alias be='bundle exec'

Install Postgresql

http://www.postgresql.org

Create Rails app

$ rails new chatty --database=postgresql

Set up db

$ be rake db:create

$ be rails generate scaffold Room topic:string

$ be rake db:migrate
==  CreateRooms: migrating ====================================================
-- create_table(:rooms)
NOTICE:  CREATE TABLE will create implicit sequence "rooms_id_seq" for serial column "rooms.id"
NOTICE:  CREATE TABLE / PRIMARY KEY will create implicit index "rooms_pkey" for table "rooms"
   -> 0.1256s
==  CreateRooms: migrated (0.1258s) ===========================================

Set up Git

$ git init
Initialized empty Git repository in /Users/jay/Projects/codeschool/heroku/chatty/.git/
$ git commit -am "Initial commit."

Set up Heroku

Create an account

https://api.heroku.com/signup

Download Heroku Toolbelt

https://toolbelt.herokuapp.com/

Set up Toolbelt credentials

$ heroku login
Enter your Heroku credentials.
Email: me@example.com
Password: 
Authentication successful.

Create Heroku App

$ heroku create --stack cedar
Creating gentle-river-1475... done, stack is cedar
http://gentle-river-1475.herokuapp.com/ | git@heroku.com:gentle-river-1475.git
Git remote heroku added

$ cat .git/config 
[core]
  ...
[remote "heroku"]
  url = git@heroku.com:gentle-river-1475.git
  fetch = +refs/heads/*:refs/remotes/heroku/*

$ git push heroku master

$ open http://gentle-river-1475.herokuapp.com/

Troubleshooting

Processes

$ heroku ps

Logs

$ heroku logs

Run tasks on server

$ heroku run rake db:migrate

Console

$ heroku run console

Config

Add S3 gem

gem 'aws-s3'

Embed video

<video controls width="512" height="288">
  <source 
    src="<%= AWS::S3::S3Object.url_for('demo.mp4', 'codetv-heroku1') %>"
    type="video/mp4"
  />
</video>

Don't do this!

config/initializers/s3.rb

require 'aws/s3'

AWS::S3::Base.establish_connection!(
  :access_key_id     => 'THISISSUPPOSEDTOBESECRET',
  :secret_access_key => 'DONTCOMMITTHISTOGITNOMATTERWHAT'
)

Do this!

config/initializers/s3.rb

require 'aws/s3'

AWS::S3::Base.establish_connection!(
  :access_key_id     => ENV['S3_KEY'],
  :secret_access_key => ENV['S3_SECRET']
)

Then use the toolbelt

$ heroku config:add S3_KEY=THISISSUPPOSEDTOBESECRET
$ heroku config:add S3_SECRET=DONTCOMMITTHISTOGITNOMATTERWHAT

Maintenance

Listing your apps

$ heroku apps
gentle-river-1475

Destroying an app server

$ heroku destroy gentle-river-1475

 !    WARNING: Potentially Destructive Action
 !    This command will affect the app: gentle-river-1475
 !    To proceed, type "gentle-river-1475" or re-run this command with --confirm gentle-river-1475

> gentle-river-1475

References

Heroku's Rails tutorial: https://devcenter.heroku.com/articles/rails3

Installing Postgresql: https://devcenter.heroku.com/articles/local-postgresql

@mhenke
Copy link

mhenke commented Jul 14, 2012

This is missing two steps. "Setup db in postgress" and "Setup db connection in Rails app" https://gist.github.com/3113042

@jaymcgavren
Copy link
Author

@mhenke Right you are, people may find that helpful. Thanks for posting the refinement!

@mhenke
Copy link

mhenke commented Jul 15, 2012

Finally got Rails/PostgeSQL work :-) Now to Heroku :-)

@mandlaanilbabu
Copy link

Can you give me one example using mongodb in heroku

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment