📖 Scenario: You are building a simple Rails web app that shows a list of products. Before you deploy it to a live server, you need to prepare the app properly so it works well for users and the server.
🎯 Goal: Learn how to prepare a Rails app for deployment by setting up the initial data, configuring environment variables, applying core deployment settings, and completing the final deployment setup.
📋 What You'll Learn
Create a basic products list in the app
Add a configuration variable for the deployment environment
Apply core deployment settings like asset precompilation
Complete the deployment setup with a production database configuration
💡 Why This Matters
🌍 Real World
Preparing a Rails app for deployment ensures it runs smoothly and securely on live servers.
💼 Career
Understanding deployment prep is key for Rails developers working on real projects that go live.
Progress0 / 4 steps
1
DATA SETUP: Create initial products data
Create a Ruby array called products with these exact strings: 'Book', 'Pen', 'Notebook'.
Ruby on Rails
Hint
Use square brackets to create an array and separate items with commas.
Create a variable called deployment_env and set it to the string 'production'.
Ruby on Rails
Hint
This variable tells Rails the app is running in production mode.
3
CORE LOGIC: Apply asset precompilation setting
Add a line to set config.assets.compile = false inside a Rails.application.configure do ... end block.
Ruby on Rails
Hint
This setting tells Rails to use precompiled assets for faster loading in production.
4
COMPLETION: Add production database configuration
Add a production key in config/database.yml with adapter: postgresql and database: myapp_production.
Ruby on Rails
Hint
This config tells Rails which database to use in production.
Practice
(1/5)
1. Why is it important to precompile assets before deploying a Rails application?
easy
A. To improve loading speed by preparing CSS and JavaScript files in advance
B. To automatically fix bugs in the application code
C. To create database tables needed for the app
D. To backup user data before deployment
Solution
Step 1: Understand what asset precompilation does
Precompiling assets means preparing CSS and JavaScript files so browsers can load them faster.
Step 2: Identify the benefit during deployment
This step reduces load time and avoids delays when users visit the app.
Final Answer:
To improve loading speed by preparing CSS and JavaScript files in advance -> Option A
Quick Check:
Precompile assets = faster page load [OK]
Hint: Precompile assets to speed up user page loads [OK]
Common Mistakes:
Thinking precompilation fixes code bugs
Confusing asset precompilation with database setup
Assuming it backs up data
2. Which command is used to migrate the database schema during Rails deployment?
easy
A. rails db:seed
B. rails server
C. rails assets:precompile
D. rails db:migrate
Solution
Step 1: Identify the command for database schema changes
The command rails db:migrate applies changes to the database structure.
Step 2: Differentiate from other commands
rails db:seed adds data, rails assets:precompile prepares assets, and rails server starts the app server.
Final Answer:
rails db:migrate -> Option D
Quick Check:
Database schema update = rails db:migrate [OK]
Hint: Use db:migrate to update database schema [OK]
Common Mistakes:
Confusing db:seed with db:migrate
Using assets:precompile for database tasks
Running rails server instead of migration
3. What will happen if you deploy a Rails app without running rails assets:precompile?
medium
A. The app will crash immediately on startup
B. The app will load assets slower or show missing styles and scripts
C. The database will not update with new tables
D. User data will be lost during deployment
Solution
Step 1: Understand the role of asset precompilation
Without precompiling, CSS and JavaScript files are not optimized for production.
Step 2: Predict the user experience
This causes slow loading or missing styles/scripts, but the app still runs.
Final Answer:
The app will load assets slower or show missing styles and scripts -> Option B
Quick Check:
Missing precompile = slow or broken assets [OK]
Hint: No precompile means broken or slow assets [OK]
Common Mistakes:
Thinking the app crashes immediately
Confusing asset issues with database problems
Assuming user data loss
4. You forgot to run rails db:migrate before deployment. What problem will most likely occur?
medium
A. The app will show errors when trying to access new database columns or tables
B. The app will load assets incorrectly
C. The server will refuse to start
D. The app will automatically migrate the database
Solution
Step 1: Identify the effect of missing migrations
Without running migrations, the database schema is outdated and missing new columns or tables.
Step 2: Understand app behavior
When the app tries to use these missing parts, it will raise errors.
Final Answer:
The app will show errors when trying to access new database columns or tables -> Option A
Quick Check:
Missing migration = database access errors [OK]
Hint: Always run db:migrate before deployment [OK]
Common Mistakes:
Thinking assets will fail due to missing migration
Assuming server won't start
Believing Rails auto-migrates on deploy
5. You want to deploy a Rails app with a new feature that requires database changes and updated assets. Which sequence of commands should you run before deployment?
hard
A. restart server, rails db:migrate, rails assets:precompile
B. rails assets:precompile, restart server, rails db:migrate
C. rails db:migrate, rails assets:precompile, restart server
D. rails db:seed, rails assets:precompile, restart server
Solution
Step 1: Apply database changes first
Run rails db:migrate to update the database schema before starting the app.
Step 2: Precompile assets after migration
Run rails assets:precompile to prepare CSS and JS files for production.
Step 3: Restart the server to apply changes
Restarting the server loads the new code, database, and assets.
Final Answer:
rails db:migrate, rails assets:precompile, restart server -> Option C
Quick Check:
Migration then precompile then restart = correct deploy steps [OK]
Hint: Migrate DB, precompile assets, then restart server [OK]
Common Mistakes:
Running precompile before migration
Restarting server before migration
Using db:seed instead of db:migrate
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.