Rails configuration step by step part-2

Laxmidevi MokhiLaxmidevi Mokhi
2 min read

βš™οΈ 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)

0
Subscribe to my newsletter

Read articles from Laxmidevi Mokhi directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Laxmidevi Mokhi
Laxmidevi Mokhi