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.
Heroku deployment in Ruby on Rails
heroku create
git push heroku main
heroku run rails db:migrate
heroku openheroku create makes a new app on Heroku and links it to your project.
git push heroku main sends your code to Heroku to deploy.
heroku create my-rails-app
# creates an app named 'my-rails-app' on Herokugit push heroku main
# deploys your current main branch to Herokuheroku run rails db:migrate
# runs database changes on Heroku serverheroku open # opens your deployed app in the browser
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.
# 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
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.
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.
Practice
Solution
Step 1: Understand Heroku's role
Heroku is a platform that hosts apps so they can be accessed online without you managing the servers.Step 2: Compare options
Options B, C, and D describe development or debugging tasks, not deployment.Final Answer:
To put the app online without managing servers -> Option AQuick Check:
Heroku deployment = online app hosting [OK]
- Confusing deployment with coding or debugging
- Thinking Heroku manages local databases
- Assuming Heroku speeds up writing code
Solution
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.Step 2: Check other options
Options A, C, and D are not valid commands for pushing code to Heroku.Final Answer:
git push heroku main -> Option DQuick Check:
Push code with git push heroku main [OK]
- Using 'heroku push' which is invalid
- Trying 'rails deploy' which doesn't exist
- Confusing Heroku CLI commands
Solution
Step 1: Understand migration on Heroku
To run migrations on Heroku's remote server, you use 'heroku run rails db:migrate'.Step 2: Check other commands
'rails db:migrate' runs locally, 'git migrate heroku' and 'heroku migrate db' are invalid commands.Final Answer:
heroku run rails db:migrate -> Option AQuick Check:
Run migrations remotely with heroku run rails db:migrate [OK]
- Running migrations locally instead of on Heroku
- Using invalid git commands for migration
- Confusing command order
Solution
Step 1: Identify common deployment errors
Forgetting to run migrations on Heroku often causes errors because the database schema is not updated.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.Final Answer:
You forgot to run database migrations on Heroku -> Option CQuick Check:
Missing migrations cause app errors on Heroku [OK]
- Ignoring migrations after deployment
- Confusing git push origin with heroku
- Blaming local environment for Heroku errors
Solution
Step 1: Understand environment variables on Heroku
Heroku uses 'heroku config:set' to securely store environment variables like API keys.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.Final Answer:
Use 'heroku config:set KEY=value' command -> Option BQuick Check:
Set env vars securely with heroku config:set [OK]
- Hardcoding keys in code risking leaks
- Relying on local .env files for Heroku
- Editing config/application.rb for secrets
Ready to prove it?
Pass the test and this pattern moves unknown
