Deploying a Rails App to Heroku — PostgreSQL

This is a tutorial I put together for some of my students…

Before beginning please have your Heroku password/login handy. You’ll need them to login to Heroku via the website and via the CLI.

* Create a new rails app. From Command line


rails new pg-test-app --database=postgresql
$ cd pg-test-app
$ rake db:create

* Add the following lines to your Gemfile and then run bundle install.

ruby '2.1.3'
gem 'rails_12factor', group: :production

* Generate a scaffold named “Post”. In the migration created by the scaffold create a column named “title”

#from command line
$ rails g scaffold Post

------
#in the generated migration file

  def change
    create_table :posts do |t|
      t.string :title
      t.timestamps null: false
    end
  end
  
  -------
#from command line
  rake db:migrate

* Create a seed file

#seeds.rb

Post.create(title:'Post 1')
Post.create(title:'Post 2')
Post.create(title:'Post 3')
Post.create(title:'Post 4')
Post.create(title:'Post 5')

------

#from command line
rake db:seed

* Start your rails server and verify that your app is working

* Go to http://www.heroku.com and create a new empty app. After creating the app a list of deployment instructions should appear similar to what you find on Github when you create an empty repo.

*If you’re not already logged into Heroku via the command line:

$ heroku login
#enter your credentials

*From command line:

$ git init
$ heroku git:remote -a YOURHEROKUAPPNAME
$ git add -A
$ git commit -m 'push to heroku'
$ git push heroku master
$ heroku run rake db:migrate
$ heroku run rake db:seed