Deploying Rails App to Heroku

Heroku account

2) You’ll be creating a new app and adding your app’s name and region.

Heroku Set up

2) In the root directory of your backend, run heroku login and login as prompted.

3) Also in the root directory of your backend Rails application, if it’s already a git repository, run heroku git:remote -a <app_name>. The app name must be the same as the name you used on Heroku.

Swtich SQLite3 default database to PostreSQL

1) Go to Rails Gemfile. Edit lines 8 and 9 from SQLite3 to # Use postgresql as the database for Active Record
gem 'pg'

2) In config/database.yml, delete all commented-out code having to do with Sqlite3 (Heroku won’t like it). Change the default adapter to postgresql, and the development database, test database, and production database to app_name_development, app_name_test, and app_name_production respectively.

3) Delete Gemfile.lock (rm Gemfile.lock). Then run bundle install. This will recreate your Gemfile AND delete your old SQLite3 database(s).

4) Run rails db:create and to create your new development and test databases.

5) Run rails db:migrate and heroku run rails db:migrate to create your tables locally and on Heroku. Check here to make sure everything looks good before moving on.

6) If you have seeds, at this point you can run rails db:seed and heroku run rails db:seed.

7) Add and commit these changes to git. If you haven’t synced Heroku with your Github, run git push heroku master. Note: this command won't run unless you installed Heroku as listed in the steps above.

Heroku & Github

Note: Your local database and your Heroku database are not the same. In order to make changes to the Heroku database. you have to preface your rails db: commands with heroku run. Ex. heroku run rails db:rollback

Checkout your fresh App, deployed on the web!

Note: Make sure you’ve specified a route. If you don’t have a “/” homepage GET route, https://app-name.herokuapp.com gives a message that the page can’t be found. https://app-name.herokuapp.com/users for example will be what you want.

Returning Users

2) In your backend’s root directory, run heroku login and login as prompted.

3) If the repo is a git repository, run heroku git:remote -a <app_name>. The app name must be the same as the name you used on Heroku.

4) Change your database from its default, SQLite3 to PostreSQL
Note: If you’ve yet to create your Rails backend, you can initialize it with a PostgreSQL database using --database=postgresql or -d=postgresql e.g. rails new <app_name> --api -d=postgresql and skip these next steps.

Note: If you have information in your database that should not be deleted, MIGRATE entries before continuing, here an helpful article.

First, go to Rails Gemfile. Edit lines 8 and 9 from SQLite3 to # Use postgresql as the database for Active Record
gem 'pg'

Next, in config/database.yml, delete all commented-out code having to do with Sqlite3 (Heroku won’t like it). Change the default adapter to postgresql, and the development database, test database, and production database to app_name_development, app_name_test, and app_name_production respectively.

Delete Gemfile.lock (rm Gemfile.lock). Then run bundle install. This will recreate your Gemfile AND delete your old SQLite3 database(s).

Then run rails db:create and to create your new development and test databases. Run rails db:migrate and heroku run rails db:migrate to create your tables locally and on Heroku. Check here to make sure everything looks good before moving on.
If you have seeds, at this point you can run rails db:seed and heroku run rails db:seed.

Lastly, add and commit these changes to git. If you haven’t synced Heroku with your Github, run git push heroku master. Note: this command won't run unless you installed Heroku as listed in the steps above.

Tip

--

--

Software Engineering student @Flatiron

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store