Trouble Deploying Rails on Heroku? Simple steps to get deployed quickly.
Recently while at Flatiron Schools coding bootcamp, we began learning about how to deploy our applications on Heroku. Bootcamp was winding down and most of the cohort’s projects were completed. For most of us it was a sign of relief. We had all made it past the giant hurdle which was phase 3 Ruby. It was at that point we all started to look into how to deploy our applications. And that’s where the frustration began. I can laugh at it now, but during that time it was no laughing matter. Everyone was stress out trying to figure out why their application would not deploy correctly on Heroku. Google searches were not very helpful and the sheer volume of different issues people were having made it hard to find something similar to the errors we were having.
It is during this stressful time that I thought to myself, “When Im done with this bootcamp, Im going to write a blog about how deploy on Heroku; adding to the thousands of other posts about the same issue. It is my hope, that if you are having the same issues we were having, this blog may help you get your application deployed.
To start, there are two major considerations you must think about from the very start of your project.
- Consider deploying both your front end and back end separately. It is a major topic of discussion online as to what is better. In my experience it was better to deploy backend and frontend separately.
- Deploy as soon as you create your project or shortly thereafter. For some reason, we found it much harder to debug and deploy a finished project.
With that out of the way, it is time to deploy your backend. I found that the same rules apply of you deploy a frontend. However for the sake of this blog, when I refer to deployment, I am talking specifically a Rails API.
IMPORTANT: Make sure that you are running Postgresql as your database. If you attempt to deploy with sqlite3 you will get errors and will not be able to proceed. Sqlite3 is the Rails default database. It is not production ready and will not allow multiple users to edit multiple files at the same time and therefore cannot be deployed to Heroku.
Step 1. Push your app to Github and then Sign up for a free Heroku account
Step 2. Once signed up, go into account settings and connect your Github account to your newly created Heroku account
Step 3. Navigate to your dashboard and click the “New” button to create a new app on Heroku.
Step 4. Next give your application an original name (this is the name that will be used in the URL) and click “create app”. After creating the app, you will be automatically taken to that app’s dashboard settings.
Step 5. Click on the “Deployment” tab
Step 6. Scroll down to the bottom of the page and click on the Github deployment method. You will see the connected status turn green like the image below.
Step 7. Next you will be asked to enter the name of the Github repository your you want connect to Heroku
Step 8. After connecting your Github repository, scroll down some more and select your deployment method. You have two options. Automatic deployment and manual deployment. I suggest using manual deployment at first. Automatic deployment will auto deploy once you push to Github.
After clicking “Deploy Branch” a window will pop up at the bottom of the page and your App will begin deploying to Heroku. You more than likely will run into an error and the app will not deploy. That is OK and is expected. We will work through those errors one at a time
The first error that you should see is “Your bundle only supports platforms [“x86_64-darwin-20”] but your local platform is x86_64-linux. Add the current platform to the lockfile with… “bundle lock — add-platform x86_64-linux” and try again.
Step 9. Open up your terminal and run “bundle lock — add-platform x86_64-linux”. While your at it also run “bundle lock — add-platform ruby”. You can then run “git status” to see the changes. Now run “git add” and git commit -m “added linux and ruby to gemfile.lock” and finally git push.
Step 10. Go back to Heroku and manual deploy again.
This time your Rails app should deploy and a view button will appear at the bottom of the deployment window. If you click on the view button you will be taken to your application and should see a blank screen. If you go to your resource end points at the end of the url (www.appname-herokuapp.com/users) or whatever your resource is you will probably see a blank screen.
Step 11. Go back to Heroku. At the top right hand corner of the screen you will see a “more” button. Click on it and go to “view logs”. You should see an error somewhere in the log that states UndefinedTable: Error: relations ‘user’ does not exist. The reason for this error is because even though you migrated your database on your local system, you still need to migrate your database on Heroku. (also remember…you must be running postgresql as stated above).
Step 12. Now all that is left to do is to go back to the ‘more’ button on the top right corner of the screen and click on “run console”. In the console type in “rails db:migrate” and Heroku should start migrating your database.
After the deployment, click the view button one more time and then got to one of your resource endpoints (/users /movies /posts etc). You should now see the JSON object showing up on your screen. Any get requests to these endpoints will now render JSON back to that fetch request.
You now have a deployed backend api. The same instructions can be followed to deploy a front end. Just remember to do it early.
If this blog post has been helpful to you in any way please consider subscribing to my blog. As always, thank your for taking the time to read my blog. Have a wonderful day!