Skip
Arish's avatar

5. Rails Directory Structure


Understanding the Rails Directory Structure

When you create a new Rails application, it generates a well-organized folder structure. Understanding this structure is essential for navigating and building Rails apps effectively.

The Complete Directory Tree

my_app/ ├── app/ # Core application code │ ├── assets/ # Static assets (images, stylesheets) │ ├── channels/ # Action Cable channels (WebSockets) │ ├── controllers/ # Handle HTTP requests │ ├── helpers/ # View helper methods │ ├── javascript/ # JavaScript files │ ├── jobs/ # Background jobs │ ├── mailers/ # Email handlers │ ├── models/ # Database models │ └── views/ # HTML templates ├── bin/ # Executable scripts ├── config/ # Configuration files ├── db/ # Database related files ├── lib/ # Custom libraries ├── log/ # Application logs ├── public/ # Static files served directly ├── storage/ # Active Storage files ├── test/ # Test files ├── tmp/ # Temporary files ├── vendor/ # Third-party code ├── Gemfile # Gem dependencies ├── Gemfile.lock # Locked gem versions └── README.md # Project documentation

The app/ Directory (Where You'll Spend Most Time)

app/controllers/

Controllers handle incoming HTTP requests and return responses:

ruby
1# app/controllers/articles_controller.rb
2class ArticlesController < ApplicationController
3  def index
4    @articles = Article.all
5  end
6  
7  def show
8    @article = Article.find(params[:id])
9  end
10end

app/models/

Models represent your data and contain business logic:

ruby
1# app/models/article.rb
2class Article < ApplicationRecord
3  belongs_to :user
4  has_many :comments
5  
6  validates :title, presence: true
7  validates :body, length: { minimum: 10 }
8end

app/views/

Views contain your HTML templates (ERB files):

erb
1<!-- app/views/articles/index.html.erb -->
2<h1>All Articles</h1>
3
4<% @articles.each do |article| %>
5  <div class="article">
6    <h2><%= article.title %></h2>
7    <p><%= article.body %></p>
8  </div>
9<% end %>

app/helpers/

Helpers contain reusable view methods:

ruby
1# app/helpers/application_helper.rb
2module ApplicationHelper
3  def formatted_date(date)
4    date.strftime("%B %d, %Y")
5  end
6end

app/mailers/

Mailers handle sending emails:

ruby
1# app/mailers/user_mailer.rb
2class UserMailer < ApplicationMailer
3  def welcome_email(user)
4    @user = user
5    mail(to: @user.email, subject: 'Welcome to My App!')
6  end
7end

app/jobs/

Jobs handle background processing:

ruby
1# app/jobs/send_newsletter_job.rb
2class SendNewsletterJob < ApplicationJob
3  queue_as :default
4  
5  def perform(user)
6    UserMailer.newsletter(user).deliver_now
7  end
8end

The config/ Directory

config/routes.rb

Defines URL routes for your application:

ruby
1# config/routes.rb
2Rails.application.routes.draw do
3  root 'home#index'
4  
5  resources :articles do
6    resources :comments
7  end
8  
9  get '/about', to: 'pages#about'
10end

config/database.yml

Database configuration for each environment:

yaml
1# config/database.yml
2default: &default
3  adapter: postgresql
4  encoding: unicode
5  pool: 5
6
7development:
8  <<: *default
9  database: myapp_development
10
11test:
12  <<: *default
13  database: myapp_test
14
15production:
16  <<: *default
17  database: myapp_production
18  username: myapp
19  password: <%= ENV['DATABASE_PASSWORD'] %>

config/application.rb

Main application configuration:

ruby
1# config/application.rb
2module MyApp
3  class Application < Rails::Application
4    config.load_defaults 7.1
5    config.time_zone = 'Eastern Time (US & Canada)'
6    config.i18n.default_locale = :en
7  end
8end

The db/ Directory

db/migrate/

Database migrations define schema changes:

ruby
1# db/migrate/20240101000000_create_articles.rb
2class CreateArticles < ActiveRecord::Migration[7.1]
3  def change
4    create_table :articles do |t|
5      t.string :title
6      t.text :body
7      t.references :user, foreign_key: true
8      
9      t.timestamps
10    end
11  end
12end

db/schema.rb

Auto-generated file showing your current database schema (never edit manually).

db/seeds.rb

Populate your database with initial data:

ruby
1# db/seeds.rb
2User.create!(
3  email: 'admin@example.com',
4  password: 'password123'
5)
6
710.times do |i|
8  Article.create!(
9    title: "Article #{i + 1}",
10    body: "This is the body of article #{i + 1}",
11    user: User.first
12  )
13end

Important Files in Root

Gemfile

Lists all gem dependencies:

ruby
1# Gemfile
2source 'https://rubygems.org'
3
4gem 'rails', '~> 7.1.0'
5gem 'pg', '~> 1.5'
6gem 'puma', '~> 6.0'
7
8group :development, :test do
9  gem 'rspec-rails'
10  gem 'factory_bot_rails'
11end
12
13group :development do
14  gem 'rubocop', require: false
15end

config.ru

Rack configuration file (used by web servers):

ruby
1# config.ru
2require_relative 'config/environment'
3run Rails.application

Understanding this structure helps you know exactly where to put your code and find what you need quickly!