Heroku Guide: How to Go Live with your Rails App

Written by Swordfish | Published 2020/12/02
Tech Story Tags: heroku | production | ruby-on-rails | databases | javascript | programming | coding | rails

TLDRvia the TL;DR App

YAML: literally meaning “Yaml ain’t markup language” can mess you up in the beginning. Rails generates this file for you and you have to change it for Heroku. YAML spacing has to be exact and it’s important to know a tab is not the same as four spaces.
Here is the obligate YMAL tutorial to read more. Just know you will have to add this to your database.yml file. My tip is to ensure your spacing matches the development: section above.
#  production:
#    url: <%= ENV['DATABASE_URL'] %>
production:
  <<: *default
  url: <%= ENV['DATABASE_URL'] %>
#username: tempGradientRailsJsonAPIJS
#password: <%= ENV['TEMPGRADIENTRAILSJSONAPIJS_DATABASE_PASSWORD']%>
Endpoints: This is easy just use [your apps name] + .herokuapp.com. So https://boiling-forest-global-warming.herokuapp.com/cities.json gave me my saved cities in my database. This makes sense as in my rails routes I have:
get '/cities.json', to: 'cities#index'
[your apps name].herokuapp.com === ‘/’ in terms of endpoints. This is your index.
Rails/Rake commands
Heroku honors most rails/rake commands. On the Heroku dashboard you can open a console and run rake db:seed for example and put your seeded data into the database. This is the entire reason I wrote this article because there are many resources telling you how to drop your Heroku database online. HEROKU DOES NOT LET YOU DROP YOUR TABLE. THERE IS NO rake db:drop ALLOWED. To do so you need the Heroku dashboard
and open your database directly. It will have three options for you.
This is if you need to drop your database and you can just click reset.
FULL TEXT ARTICLE:
Development includes designing and coding out a full stack Ruby on Rails REST api with a JavaScript front end. That’s fine while you are testing your localhost:3000 endpoints and even getting data from a 3rd party API(openweathermap). I wrote about this here: Development of Boiling-Forest.
Production could mean many different things. Let's ask Wikipedia: production in computer science. Okay that is perfect because just it is just like Computer Science; confusing and not really for human eyes. I’ll use “Putting a site live on the Web” as our definition for production. Heroku is a cloud based platform that will host your application live on the web for you. Under the hood Heroku uses AWS and will take your code, package it, modify it, and spin up a live working version. There is a lot you have to do going from localhost:3000 to https//your-heroku-app/herokuapp.com.
First you need to get your database.yml file in order.
When you first generated your rails app it created a lot for you. This includes the database.yml file which contains sections:
default:
development: 
test: 
production:
YAML cannot have any extra spaces(no white space) which will crash your Heroku app. Rails does a fairly good job having it almost ready. The only adjustment will be adding and removing a few parts of the production section. The file itself has a comment on this exactly as follows:
# On Heroku and other platform providers, you may have a full connection URL
# available as an environment variable. For example:
#
#   DATABASE_URL="postgres://myuser:mypass@localhost/somedatabase"
#
# You can use this database configuration with:
#
#  production:
#    url: <%= ENV['DATABASE_URL'] %>
Literally make your production section exactly that and only that. No extra white space and no extra password or username variables.
production:
  <<: *default
  url: <%= ENV['DATABASE_URL'] %>
There should be two indent spaces under the production:
Once you have database.yml file hooked up to hit the proper production database you are going to need to adjust your endpoints.
Endpoints are essentially the URL/URI of the resources you want. The the telephone number of the friend you want to call or your address when you want to go home; This is where you want to go. In development you were hitting localhost:3000. Now that you are going into production you are no longer hitting your local server running on your local machine. This is actually easier than most help articles indicate and it makes sense. Your endpoint is going to be your-apps-name.herokuapp.com. This is the equivalent of ‘/’. So my endpoint was https://boiling-forest-global-warming.herokuapp.com. I did a little trial and error and put https://boiling-forest-global-warming.herokuapp.com/cities.json in the address bar of my browser. What did I get in return?
[{"id": 1,"name": "Phoenix","region": "southwest","created_at": "2020-10-27T13:43:21.239-07:00","updated_at": "2020-10-27T13:43:21.239-07:00","fetchURL": "https://api.openweathermap.org/data/2.5/weather?q=Phoenix,us&units=imperial&APPID=fe2a775f427aa5fc92ce0379937b9ee9"},{"id": 2,"name": "Columbus","region": "Midwest","created_at": "2020-10-27T13:43:21.250-07:00","updated_at": "2020-10-27T13:43:21.250-07:00","fetchURL": "https://api.openweathermap.org/data/2.5/weather?q=Columbus,us&units=imperial&APPID=fe2a775f427aa5fc92ce0379937b9ee9"},{"id": 3,"name": "Boston","region": "east","created_at": "2020-10-27T13:43:21.258-07:00","updated_at": "2020-10-27T13:43:21.258-07:00","fetchURL": "https://api.openweathermap.org/data/2.5/weather?q=Boston,us&units=imperial&APPID=fe2a775f427aa5fc92ce0379937b9ee9"},{"id": 4,"name": "Orlando","region": "southeast","created_at": "2020-10-27T13:43:21.267-07:00","updated_at": "2020-10-27T13:43:21.267-07:00","fetchURL": "https://api.openweathermap.org/data/2.5/weather?q=Orlando,us&units=imperial&APPID=fe2a775f427aa5fc92ce0379937b9ee9"},{"id": 5,"name": "Seattle","region": "northwest","created_at": "2020-10-27T13:43:21.275-07:00","updated_at": "2020-10-27T13:43:21.275-07:00","fetchURL": "https://api.openweathermap.org/data/2.5/weather?q=Seattle,us&units=imperial&APPID=fe2a775f427aa5fc92ce0379937b9ee9"}]
Perfection: I got a JSON response and my PostgreSQL database on Heroku is working!
So my endpoints are up and serving out data to the real world.
Now once your endpoints are hitting and you have your database.yml file in order you might want to drop your database. I know in development I did this a lot. Sometimes you need to clear out your database and start over. There are many articles describing how to do this. I want to make this crystal clear. HEROKU DOES NOT LET YOU DROP YOUR DATABASE. You can waist your time trying but it will now work. You have to go to your Heroku dashboard and then open your database here:
Now from here you mush RESET your database.
I created this because there is a ton of information on dropping your database with Heroku. You will waist your time and all you have to do is go to the Heroku dashboard. I hope this helps and will save you your time. Code on Happy Coder, Code on.
Also published on: https://software-for-humanity.medium.com/heroku-production-tldr-how-to-go-live-with-your-rails-app-7a7aabf22c4

Written by Swordfish | JS, React, Redux, Ruby, Rails, SQL, Python
Published by HackerNoon on 2020/12/02