Understanding Rails

Understanding Rails · Season 2: Getting started

Environments, configuration and credentials

Episode 16 · overview · about 3 minutes · Rails 8.1 · by

How was this episode?

The series' author reads every note, and no one else sees it. When something is wrong or unclear, it gets fixed and the episode re-cut. How this series stays right.

Was it clear?

Not endorsed yet. Are you a Rails developer? What's an endorsement?

Endorse this version To endorse: create an account, confirm your email, then link GitHub.

What changed

  • Version 2, November 20, 2026: Fixed the table: in tests, Rails shows error pages only for errors it knows how to rescue.

development, test and production; config/; encrypted credentials.

Free to watch. If it helped, support the series.

Transcript

0:00 Environments and credentials

Episode sixteen. Environments, configuration, and credentials. The same app runs three ways, reads its settings from config, and keeps its secrets locked.

0:15 Three environments

Every Rails app has three environments: development on your computer, test while your tests run, and production on a server. Rails dot env tells you which one you're in.

0:27 Choosing one

The Rails env variable chooses. Leave it out, and you get development. Bin slash rails test sets test for you.

0:36 application.rb and environments/

Config slash application dot rb holds what every environment shares. Then each file in config slash environments changes only what its environment needs.

0:47 How they differ

Compare them. Development reloads your code as you save it, and shows full error pages. Production loads all of it once, at boot, hides error details from visitors, and logs at info level.

1:01 dev:cache

Development doesn't cache, so you always see fresh pages. To try caching there, bin slash rails dev colon cache turns it on, and off again.

1:12 Your own settings

Your own settings can live in config too. A yaml file with a section for each environment, read with config for, gives each environment its own values.

1:23 Secrets

Secrets are different. Passwords and API keys must never sit in your code, in plain text. That's what credentials are for.

1:33 credentials.yml.enc

Credentials dot yaml dot enc is encrypted. It's safe to commit: without the key, it's noise.

1:41 master.key

Master dot key unlocks it. Rails keeps the key out of git for you. On a server, you pass it in the Rails master key environment variable instead.

1:52 credentials:edit

To change a secret, run bin slash rails credentials colon edit. It opens them, decrypted, in your editor, and encrypts them again when you close it.

2:02 Reading a credential

In code, read one through Rails dot application dot credentials. Dig reaches inside a group, like the access key id under aws.

2:13 Per-environment credentials

Want different secrets for production? Add dash dash environment production, and production gets its own file, and its own key.

2:22 Where things go

So: what's shared goes in application dot rb, what differs goes in each environment's file, and secrets go in credentials. Never in the code.

Next: migrations in depth.

Check your understanding

  1. RAILS_ENV is not set. You run bin/rails runner 'puts Rails.env'. What prints?

    • production
    • development
    • test
    • Nothing, Rails raises an error
    Show answer

    development. Rails.env reads RAILS_ENV, then RACK_ENV, and falls back to development. Source: Rails.env (Rails source, v8.1.4.1).

  2. config/notes.yml has a shared section with title: Notes, and a development section with per_page: 5. In development, what does config_for(:notes) give you?

    • Only per_page: 5
    • Only title: Notes
    • Both title: Notes and per_page: 5
    • An error, because shared is not an environment
    Show answer

    Both title: Notes and per_page: 5. config_for merges the shared section into the current environment's section. Source: Custom Configuration, Configuring Rails Applications (Rails Guides 8.1).

  3. Your server has no config/master.key file. How does Rails get the key to read credentials there?

    • From the RAILS_MASTER_KEY environment variable
    • It reads the credentials without a key
    • From secret_key_base in the Gemfile
    • It downloads the key from your git repository
    Show answer

    From the RAILS_MASTER_KEY environment variable. Rails uses config/master.key, or else looks for ENV["RAILS_MASTER_KEY"]. Source: Custom Credentials, Securing Rails Applications (Rails Guides 8.1).

  4. You ran bin/rails credentials:edit --environment production. In production, which file does Rails read secrets from?

    • config/credentials.yml.enc
    • config/master.key
    • config/environments/production.rb
    • config/credentials/production.yml.enc
    Show answer

    config/credentials/production.yml.enc. Rails looks first for a credentials file named for the current environment, and uses config/credentials.yml.enc only if that file does not exist. Source: Rails::Application#credentials (Rails API 8.1).

Sources

Every example in this episode was run on Rails 8.1.4.1 and Ruby 4.0.2.

Comments

Sign in to comment or sign up.