How do you configure different environments in a Rails application (e.g., development, test, production)?

Managing different environments is a cornerstone of a successful Ruby on Rails application. Rails offers a sensible default for handling multiple environments such as development, test, and production, each designed to cater to specific stages in the process of taking an application from a simple idea to a robust, deployable product. For more on Rails configuration principles, check out our guide on convention over configuration in Rails.

Understanding Rails Environments

In Rails, an environment refers to the various setups and configurations needed for running an application under different circumstances. Rails has three main environments by default:

  1. Development: The environment where the application is actively being developed. It includes configurations that aid debugging and allow for rapid changes. For more on development setup, see our guide on what are initializers in Rails and how to use them.

  2. Test: This environment is used for running test cases. The configuration is designed to be isolated from other environments with default database cleaning. For testing best practices, check out our guide on how to test controllers in Rails.

  3. Production: In this environment, performance and security are optimized. Error messages are minimal and logging is tuned to reduce overhead. For production deployment, see our guide on deploying Rails applications to production.

Each environment can have its own database configuration, gem dependencies, and specific runtime variables.

Configuring Environments

Rails provides a config/environments directory where each environment has its own configuration file: development.rb, test.rb, and production.rb. These files are loaded based on the environment you are running. For more on configuration files, explore our guide on the purpose of config/environments directory.

ruby
1# config/environments/development.rb
2Rails.application.configure do
3 config.cache_classes = false
4 config.eager_load = false
5 config.consider_all_requests_local = true
6 # Add more development configurations here
7end
8
9# config/environments/test.rb
10Rails.application.configure do
11 config.cache_classes = true
12 config.eager_load = false
13 config.consider_all_requests_local = true
14 config.action_mailer.delivery_method = :test
15 # Add more test configurations here
16end
17
18# config/environments/production.rb
19Rails.application.configure do
20 config.cache_classes = true
21 config.eager_load = true
22 config.consider_all_requests_local = false
23 config.log_level = :info
24 config.public_file_server.enabled = ENV['RAILS_SERVE_STATIC_FILES'].present?
25 # Add more production configurations here
26end
27

Development Environment Configuration

In the development environment, real-time changes are crucial. Therefore, configurations like cache_classes are set to false to reload code for each request, making it easier to see changes immediately. For more on caching, see our guide on configure and use ActiveSupport::Cache::Store effectively.

Test Environment Configuration

The test environment configuration is optimized for running automated tests. It is set to cache_classes to ensure that all test files are loaded only once for speed, and the environment is separated to ensure tests do not affect the development or production databases. For more on testing, check out our guide on testing Rails applications.

Production Environment Configuration

The production environment prioritizes efficiency and security. The configurations might include:

Managing Environment Variables

Environment variables are central to configuring Rails applications. They can be managed using tools like dotenv-rails gem, which loads environment variables from a .env file. For more on environment variables, see our guide on configure and use Rails log levels.

env
1# .env
2DATABASE_USERNAME=myuser
3DATABASE_PASSWORD=mypass
4SECRET_KEY_BASE=your_secret_key_base
5

This approach ensures sensitive information does not get hardcoded into your application but rather remains changeable outside of your codebase.

Switching Environments

Rails allows switching between environments using the RAILS_ENV environment variable. This can be done on the command line:

shell
1rails server -e production
2rails console RAILS_ENV=test
3

Learn more about RAILS_ENV here.

Conclusion

Configuring different environments in a Rails application is crucial for seamless application development, testing, and deployment. By isolating settings and behavior specific to development, testing, and production, you can ensure that the code you write is robust, your tests are reliable, and your application functions flawlessly in production. Remember to periodically revisit your environment configurations as your application grows to ensure they continue to meet your needs. For more details on Rails best practices, visit Rails Guides.

Suggested Articles