0
0
Ruby on Railsframework~5 mins

Heroku deployment in Ruby on Rails

Choose your learning style9 modes available
Introduction

Heroku deployment lets you put your Rails app online so anyone can use it. It makes sharing your work easy without needing your own server.

You want to share your Rails app with friends or users on the internet.
You need a simple way to host your app without managing servers.
You want to test your app in a real online environment.
You want to deploy updates quickly and easily.
You want to use a free or low-cost hosting option for small projects.
Syntax
Ruby on Rails
heroku create
git push heroku main
heroku run rails db:migrate
heroku open

heroku create makes a new app on Heroku and links it to your project.

git push heroku main sends your code to Heroku to deploy.

Examples
You can name your app to make it easier to find.
Ruby on Rails
heroku create my-rails-app
# creates an app named 'my-rails-app' on Heroku
This uploads your code so Heroku can run it.
Ruby on Rails
git push heroku main
# deploys your current main branch to Heroku
Apply your database updates after deployment.
Ruby on Rails
heroku run rails db:migrate
# runs database changes on Heroku server
Quickly check your live app after deployment.
Ruby on Rails
heroku open
# opens your deployed app in the browser
Sample Program

This sequence shows the basic steps to deploy a Rails app to Heroku. First, you prepare your code with git. Then you create the Heroku app, push your code, update the database, and finally open the app online.

Ruby on Rails
# 1. Initialize git and commit your Rails app
$ git init
$ git add .
$ git commit -m "Initial commit"

# 2. Create Heroku app
$ heroku create

# 3. Push code to Heroku
$ git push heroku main

# 4. Run database migrations
$ heroku run rails db:migrate

# 5. Open your app in browser
$ heroku open
OutputSuccess
Important Notes

Make sure your Rails app has a Gemfile and uses PostgreSQL, as Heroku supports it by default.

Check your config/database.yml to use environment variables for production database settings.

Use heroku logs --tail to see live logs if your app has issues after deployment.

Summary

Heroku deployment puts your Rails app online easily without managing servers.

Use git commands to push your code and run migrations on Heroku.

Open your app in a browser to see it live after deployment.