Deploy Rails App to Heroku
To deploy Rails app to Heroku there is few steps to do before it works. If Rails app use SQLite database, it needs to change to Postgresql SQLite isn’t support by Heroku. The second thing is make sure the Ruby version is supported by Heroku. Latest Ruby version is good for the latest ruby features such as better security but it may not fully work with Heroku. Check here for supported Ruby versions.
Semantic versions number explains, example version 2.7.4, the first number in this case 2 is refers to major version, the second 7 refers to minor version, and the last is patches. When the major version number change, it is potentially introduce problems. When the minor version is change, new features are added and library may updated. In that case, some previous version may no longer work. When the patches are changed, security of that minor version is updated. It won’t change features or functions on that particular version.
Switch Ruby versions with rvm
If you have most recent version of Ruby, it may not support by Heroku. Check dyour app Ruby version with rvm(Ruby version Manager).
rvm --list # to list of Ruby versions installed on your machine
rvm use <Ruby version> --default # set default ruby version
Database
Like I said earlier, Postgres is the database for Rails app. It is a good practice to maintain same database version but different versions may not effect to the app function. The benefit of using Postgres is Postgres support to store json on a column which not allow with SQLite.
If Rails app is created with SQLite, make sure install Postgresql before deploy. to do that add :production group and add pg in it.
In Gemfile add following code,
group :production do gem "pg", "~> 1.2"
end
Add sqlite3 gem in :development, :test group to use SQLite just for production.
If backend and frontend on same origin, gem ‘rock-cors’ is not necessary to enable.
Create package.json file in project root folder and add following code
{"name": "inventory_management_system","version": "1.0.0","description": "[App name]","engines": {"node": "[add app's node version]"},"scripts": {"clean": "rm -rf public","build": "npm install --prefix client && npm run build --prefix client","deploy": "cp -a client/build/. public/","heroku-postbuild": "npm run clean && npm run build && npm run deploy"},"author": "[author name]"}
To build the app run this in terminal [ npm start heroku-postbuild ]
Important!!
If code need to modify or fix run npm start — prefix client
add Procfile in project root folder and add following code
web: bundle exec rails s
release: bin/rake db:migrate
Login to your Heroku account from terminal
heroku login
Then create heroku app
heroku create
this command will create app on heroku remote
finally push to heroku
git push heroku main
Once deploying is finished, type
heroku open
If you want to run rails console on heroku type following command
heroku run rails c