Deploying a Ruby on Rails API to Heroku

ljg2gb

ljg2gb

Posted on June 30, 2020

Deploying a Ruby on Rails API to Heroku

First Time Users

Note: These are tailored instructions for deploying a Ruby on Rails API to Heroku for the first time. If you already have a Heroku account scroll to the Returning Users section :)

Creating a Heroku account

1) This part is self-explanatory. Here's the link: https://signup.heroku.com/

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

Setting up Heroku

1) Install Heroku CLI. On a Mac, if you have homebrew, run brew tap heroku/brew && brew install heroku in the terminal, otherwise check out the documentation.

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.

Changing 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.

Another note: If you have information in your database that should not be deleted, MIGRATE entries before continuing. I found an article that looks helpful here(although I haven't needed to do this so take the recommendation with a grain of salt!).

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.

6) 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.

Syncing Heroku w/ Github

1) If you have a GitHub repo that you'd like to link to Heroku, it's super easy.

2)In your Heroku dashboard, under the Deployment Method section, you'll see the GitHub logo w/ a button "Connect to GitHub". You'll follow the prompts, sign in to GitHub, and be all set. Now, when you push new code up to GitHub, it will update your Heroku app as well.

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 API, deployed on the web!

Use the "Open App" button in your dashboard's top right corner.

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

1) Log back into Heroku and create a new app by adding your app's name and region.

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.

Another note: If you have information in your database that should not be deleted, MIGRATE entries before continuing. I found an article that looks helpful here(although I haven't needed to do this so take the recommendation with a grain of salt!).

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.

5) Sync Heroku w/ Github

If you have a GitHub repo that you'd like to link to Heroku, it's super easy. In your Heroku dashboard, under the Deployment Method section, you'll see the GitHub logo w/ a button "Connect to GitHub". Follow the prompts, sign in to GitHub, and you're all set! When you push new code up to GitHub, it will update your Heroku app as well.

Note: Remember that your local database and your Heroku database are not the same. To make changes to the Heroku database, preface your rails db: commands with heroku run. Ex. heroku run rails db:rollback

Debugging Tip

heroku logs --tail will open up a server that logs requests to your Heroku backend. You can even use heroku run rails c to check User.all, etc.

Happy coding! :)

💖 💪 🙅 🚩
ljg2gb
ljg2gb

Posted on June 30, 2020

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related