Bird
Raised Fist0
Ruby on Railsframework~30 mins

Heroku deployment in Ruby on Rails - Mini Project: Build & Apply

Choose your learning style10 modes available
Deploying a Ruby on Rails App to Heroku
📖 Scenario: You have built a simple Ruby on Rails web application on your computer. Now, you want to make it available on the internet so your friends can visit it anytime. Heroku is a platform that helps you put your app online easily.
🎯 Goal: Deploy a basic Ruby on Rails application to Heroku so it can be accessed via a web URL.
📋 What You'll Learn
Create a new Rails app with a homepage
Add a Procfile to tell Heroku how to run the app
Set the Ruby version in Gemfile
Initialize a Git repository and commit the code
Create a Heroku app using the Heroku CLI
Push the code to Heroku to deploy
Open the deployed app in a browser
💡 Why This Matters
🌍 Real World
Deploying web applications to Heroku is a common way to share your projects with others online quickly and easily.
💼 Career
Many companies use Heroku or similar platforms to host their web apps. Knowing how to deploy apps is a key skill for web developers.
Progress0 / 4 steps
1
Create a new Rails app with a homepage
Run rails new myapp to create a new Rails app folder called myapp. Then, inside myapp, create a controller named home with an index action by running rails generate controller home index. Finally, set the root route to home#index in config/routes.rb by adding root 'home#index'.
Ruby on Rails
Hint

Use rails generate controller home index to create the controller and view. Then set the root route in config/routes.rb.

2
Add a Procfile and set Ruby version
Create a file named Procfile in the root of your app folder with the line web: bundle exec puma -C config/puma.rb. Then, open Gemfile and add the line ruby '3.2.2' at the top to specify the Ruby version.
Ruby on Rails
Hint

The Procfile tells Heroku how to start your app. Setting Ruby version in Gemfile helps Heroku use the right Ruby.

3
Initialize Git and commit your code
Run git init in your app folder to start a Git repository. Then run git add . to stage all files. Finally, commit the files with git commit -m 'Initial commit'.
Ruby on Rails
Hint

Git tracks your code changes. Heroku uses Git to get your app code.

4
Create Heroku app and deploy
Run heroku create myapp-demo to create a Heroku app named myapp-demo. Then push your code with git push heroku main. After deployment, open your app in the browser with heroku open.
Ruby on Rails
Hint

Use the Heroku CLI commands to create the app, push code, and open the live site.

Practice

(1/5)
1. What is the main purpose of deploying a Rails app to Heroku?
easy
A. To put the app online without managing servers
B. To write Ruby code faster
C. To create a local database
D. To debug Rails code automatically

Solution

  1. Step 1: Understand Heroku's role

    Heroku is a platform that hosts apps so they can be accessed online without you managing the servers.
  2. Step 2: Compare options

    Options B, C, and D describe development or debugging tasks, not deployment.
  3. Final Answer:

    To put the app online without managing servers -> Option A
  4. Quick Check:

    Heroku deployment = online app hosting [OK]
Hint: Heroku hosts apps online, no server setup needed [OK]
Common Mistakes:
  • Confusing deployment with coding or debugging
  • Thinking Heroku manages local databases
  • Assuming Heroku speeds up writing code
2. Which command is used to push your Rails app code to Heroku?
easy
A. heroku push
B. heroku deploy app
C. rails deploy
D. git push heroku main

Solution

  1. Step 1: Identify the correct git command

    Heroku uses git to receive code. The correct command is 'git push heroku main' to push the main branch.
  2. Step 2: Check other options

    Options A, C, and D are not valid commands for pushing code to Heroku.
  3. Final Answer:

    git push heroku main -> Option D
  4. Quick Check:

    Push code with git push heroku main [OK]
Hint: Use git push heroku main to deploy code [OK]
Common Mistakes:
  • Using 'heroku push' which is invalid
  • Trying 'rails deploy' which doesn't exist
  • Confusing Heroku CLI commands
3. After pushing your Rails app to Heroku, which command runs database migrations on Heroku?
medium
A. heroku run rails db:migrate
B. rails db:migrate
C. git migrate heroku
D. heroku migrate db

Solution

  1. Step 1: Understand migration on Heroku

    To run migrations on Heroku's remote server, you use 'heroku run rails db:migrate'.
  2. Step 2: Check other commands

    'rails db:migrate' runs locally, 'git migrate heroku' and 'heroku migrate db' are invalid commands.
  3. Final Answer:

    heroku run rails db:migrate -> Option A
  4. Quick Check:

    Run migrations remotely with heroku run rails db:migrate [OK]
Hint: Run migrations remotely with heroku run rails db:migrate [OK]
Common Mistakes:
  • Running migrations locally instead of on Heroku
  • Using invalid git commands for migration
  • Confusing command order
4. You pushed your Rails app to Heroku but see an error when opening the app URL. What is a common cause?
medium
A. You did not install Ruby locally
B. You used 'git push origin main' instead of 'git push heroku main'
C. You forgot to run database migrations on Heroku
D. You opened the wrong browser

Solution

  1. Step 1: Identify common deployment errors

    Forgetting to run migrations on Heroku often causes errors because the database schema is not updated.
  2. Step 2: Evaluate other options

    You used 'git push origin main' instead of 'git push heroku main' means code wasn't pushed to Heroku, but question says code was pushed. Options A and D are unrelated to deployment errors.
  3. Final Answer:

    You forgot to run database migrations on Heroku -> Option C
  4. Quick Check:

    Missing migrations cause app errors on Heroku [OK]
Hint: Run migrations after push to avoid errors [OK]
Common Mistakes:
  • Ignoring migrations after deployment
  • Confusing git push origin with heroku
  • Blaming local environment for Heroku errors
5. You want to deploy a Rails app with environment variables for API keys on Heroku. Which is the correct way to set them before deployment?
hard
A. Add keys to config/application.rb file
B. Use 'heroku config:set KEY=value' command
C. Store keys in a local .env file only
D. Push keys directly in your code files

Solution

  1. Step 1: Understand environment variables on Heroku

    Heroku uses 'heroku config:set' to securely store environment variables like API keys.
  2. Step 2: Evaluate other options

    Adding keys to config/application.rb or pushing keys in code risks exposure. Local .env files are not sent to Heroku.
  3. Final Answer:

    Use 'heroku config:set KEY=value' command -> Option B
  4. Quick Check:

    Set env vars securely with heroku config:set [OK]
Hint: Use heroku config:set to add env variables securely [OK]
Common Mistakes:
  • Hardcoding keys in code risking leaks
  • Relying on local .env files for Heroku
  • Editing config/application.rb for secrets
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard