Note: This post is intended as a supplement to WTF Just Happened? A Quick Tour of Your First Rails App.
Important Configuration Files
People often say that Rails favours convention over configuration. However, that doesn't mean you can forget about configuration altogether.
You may have noticed that your new Rails project comes with a
config directory that contains a lot of files. So much for convention over configuration! Many of these files you don't need to worry about, but there are times when you might need to make a few changes to customise the behaviour of your application.
Here we will discuss the main files, their purpose, and some customisations you might wish to make.
Starting Your Application
There are three main files involved in starting up your Rails application:
config/boot.rbSets up Bundler and the load paths.
config/application.rbThis contains any application-wide configuration settings for Rails and its components. e.g. Time Zone or Localisation
config/environment.rbThis loads your Rails application and runs all the initializers
The initializers directory contains all the configuration settings that need to be loaded after the framework and the gems are loaded. There are ten initializer files that are included in all Rails applications but you can add your own as required. It is considered best practise to group related initializers together into their own separate - and appropriately named - initializer file, rather than jam them all together into one large general file.
The ten initializer files that Rails provides for you are:
- Application Controller Renderer Allows you to render arbitrary templates without having to be in a controller action.
- Backtrace Silencers Allows you to modify the way that backtraces are shortened.
- Cookies Serialiser Allows you to specify the way cookies are serialised.
- Filter Parameter Logging Allows you to specify which parameters to filter out of your log files.
- Inflections Contains the default inflections for pluralisation and singularisation of uncountable words.
- Custom MIME Types Rails supports a standard set of MIME types. Adding to this file allows you to specify additional MIME types for your application to respond to.
- New Framework Defaults Contains migration options to make upgrading Rails versions a little easier.
- Session Store Allows you to specify what class to use to store the session.
Every Rails application comes pre-configured with three different environments for operation: development, test, and production. Each environment has its own configuration file which specifies how things should work in that environment. These files can be found in the
config/environments directory and are called,
production.rb. They include details such as which database to connect to, and whether classes in your application should be reloaded with each request. We use an environment variable called RAILS_ENV to determine which environment we are in.
Other useful files
config/database.yml: contains all the settings required by Active Record to connect to a database.
config/secrets.yml: stores your application's sensitive data, such as access keys and passwords that are required for external APIs.
config/routes.rb: provides a mapping between a URL and a controller action. This is covered in more detail in Rails Routing.
Spring Application Preloader
Although not a configuration file, Spring works closely with Rails configurations and deserves a mention. It is an application preloader that ships with Rails. During development, your application runs in the background. Spring monitors the
initializers directories for any changes. When a change is detected, it will automatically restart your application. It will also restart your application if any gem dependencies are changed during development.
For more information
For a comprehensive (and more advanced) guide on configuring Rails, please refer to the Rails Guides.
Five UX Tips for Better Dashboards
Dissecting Code With Ruby's caller Method
Handling deletes with Null Object Pattern in Ruby
reinteractive is Australia’s largest dedicated Ruby on Rails development company. We don’t cut corners and we know what we are doing.
We are an organisation made up of amazing individuals and we take pride in our team. We are 100% remote work enabling us to choose the best talent no matter which part of the country they live in. reinteractive is dedicated to making it a great place for any developer to work.
Webinars are our online portal for tips, tricks and lessons learned in everything we do. Make the most of this free resource to help you become a better developer.
The Ruby on Rails Installfest includes a full setup of your development environment and step-by-step instructions on how to build your first app hosted on Heroku. Over 1,800 attendees to date and counting.
The Ruby on Rails Development Hub is a monthly event where you will get the chance to spend time with our team and others in the community to improve and hone your Ruby on Rails skills.