Rails configuration step by step part-2

βοΈ Ruby on Rails Configuration β Step-by-Step Guide (Part 2)
Welcome back to my Rails Configuration blog series! In Part 1, we covered installing Ruby, Rails, setting up your first app, and basic directory structure.
In this post, we'll dive into configuring your Rails app the right way before writing any business logic. Let's get started! π
---
π§© Step 5: Configure Environment Files
Rails has three default environments:
development
test
production
π Where are these located?
config/environments/development.rb
config/environments/test.rb
config/environments/production.rb
You can modify settings like:
config.cache_classes = false
config.eager_load = false
config.consider_all_requests_local = true
These settings let you:
Enable/disable caching
Load code eagerly/lazily
Control error visibility
π Tip: Never display full error reports in production!
---
π Step 6: Set Up Credentials and Secrets
Rails 5.2+ uses encrypted credentials.
To edit credentials:
EDITOR="code --wait" bin/rails credentials:edit
This opens an encrypted file. Store sensitive info like:
aws:
access_key_id: YOUR_KEY
secret_access_key: YOUR_SECRET
π‘ These are accessed in code via:
Rails.application.credentials.aws[:access_key_id]
---
π§° Step 7: Add Essential Gems
Here are some gems most projects need:
β Authentication
gem 'devise'
β Authorization
gem 'pundit' # or 'cancancan'
β Pagination
gem 'kaminari' # or 'will_paginate'
β Background Jobs
gem 'sidekiq'
After editing the Gemfile:
bundle install
---
π Step 8: Setup CORS (Cross-Origin Resource Sharing)
If youβre building an API or integrating with a frontend (like React):
Edit config/initializers/cors.rb:
Rails.application.config.middleware.insert_before 0, Rack::Cors do
allow do
origins '*' # change this in production!
resource '*', headers: :any, methods: (:get, :post, :patch, :put)
Subscribe to my newsletter
Read articles from Laxmidevi Mokhi directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
