The config folder in a Rails app holds settings that tell your app how to behave. It keeps important setup details organized in one place.
Config folder purpose in Ruby on Rails
config/ environments/ initializers/ locales/ routes.rb database.yml credentials.yml.enc
The config folder contains subfolders and files for different settings.
Files like routes.rb define how web addresses connect to your code.
config/routes.rb # Defines URL paths and which controller actions handle them Rails.application.routes.draw do root 'home#index' end
config/database.yml
# Sets database connection details for each environment
development:
adapter: sqlite3
database: db/development.sqlite3config/environments/production.rb
# Settings specific to the production environment
Rails.application.configure do
config.cache_classes = true
endThis example shows how the config/routes.rb file connects the URL '/welcome' to the welcome action in PagesController. When you visit '/welcome', it shows a simple text message.
# config/routes.rb Rails.application.routes.draw do get '/welcome', to: 'pages#welcome' end # app/controllers/pages_controller.rb class PagesController < ApplicationController def welcome render plain: 'Hello from the welcome page!' end end
Keep config files organized to avoid confusion as your app grows.
Use environment-specific files to change settings safely without affecting other environments.
Never put sensitive keys directly in config files; use encrypted credentials instead.
The config folder holds all setup details for your Rails app.
It helps keep your app organized and easy to change.
Understanding config files helps you control how your app works in different situations.
Practice
config folder in a Rails application?Solution
Step 1: Understand the role of the config folder
The config folder contains files that set up how the Rails app runs, like database settings and environment variables.Step 2: Compare with other folders
Folders like views hold templates, and assets hold images/stylesheets, so they are not for configuration.Final Answer:
To store setup and configuration files that control how the app behaves -> Option CQuick Check:
Config folder = setup files [OK]
- Confusing config with views or assets folders
- Thinking config stores user data
- Assuming config holds templates
config folder in a Rails app?Solution
Step 1: Identify typical config files
Theroutes.rbfile defines URL routes and is always inside the config folder.Step 2: Recognize other file locations
Files likeapplication.html.erbare views,user.rbis a model, andstyle.cssis an asset, so they are not in config.Final Answer:
routes.rb -> Option AQuick Check:
Routes file is in config folder [OK]
- Mixing view files with config files
- Thinking models are in config
- Confusing CSS files with config files
config/database.yml file:development: adapter: sqlite3 database: db/development.sqlite3 production: adapter: postgresql database: myapp_prod
What does this configuration do?
Solution
Step 1: Read the environment keys
The file defines settings separately for development and production environments.Step 2: Check adapter values per environment
Development uses SQLite3 adapter; production uses PostgreSQL adapter.Final Answer:
Sets SQLite for development and PostgreSQL for production environments -> Option BQuick Check:
Different adapters per environment = Sets SQLite for development and PostgreSQL for production environments [OK]
- Assuming one adapter applies to all environments
- Thinking multiple adapters cause errors
- Ignoring environment separation
Missing `secret_key_base` for 'production' environmentWhich config file should you check to fix this?
Solution
Step 1: Understand the error message
The error says the secret key base is missing for production, which is used for security.Step 2: Identify where secret keys are stored
Secret keys likesecret_key_baseare stored inconfig/secrets.yml.Final Answer:
config/secrets.yml -> Option AQuick Check:
Secret keys live in secrets.yml [OK]
- Checking routes.rb for secret keys
- Looking in database.yml for secret_key_base
- Confusing application.rb with secrets.yml
Solution
Step 1: Identify environment-specific config files
Rails uses separate files inconfig/environments/to set different behaviors per environment.Step 2: Understand roles of other files
routes.rbsets URL routes,database.ymlsets database connections,initializersrun on all environments, andapplication.rbis general config.Final Answer:
config/environments/development.rb and config/environments/production.rb -> Option DQuick Check:
Environment-specific settings go in environments folder [OK]
- Editing only application.rb for environment changes
- Changing routes.rb for environment behavior
- Assuming initializers are environment-specific
Ready to prove it?
Pass the test and this pattern moves unknown
