๐Ÿ“

Rails Directory Structure

Roles and conventions for each project folder

When you create a Rails project with rails new, a standard directory structure is generated.

app/ โ€” Core application code

  • app/models/ โ€” ActiveRecord models (data + business logic)

  • app/controllers/ โ€” Request-handling controllers

  • app/views/ โ€” ERB templates (HTML)

  • app/helpers/ โ€” View helper methods

  • app/mailers/ โ€” Email delivery

  • app/jobs/ โ€” Background jobs

  • app/channels/ โ€” Action Cable (WebSocket)

  • app/javascript/ โ€” JavaScript files

  • app/assets/ โ€” Images, fonts, and other static files

config/ โ€” Configuration files

  • routes.rb โ€” URL โ†’ Controller mapping

  • database.yml โ€” DB connection info

  • credentials.yml.enc โ€” Encrypted secrets

  • initializers/ โ€” Settings executed at Rails startup

db/ โ€” Database

  • migrate/ โ€” Migration files (schema change history)

  • schema.rb โ€” Current schema snapshot

  • seeds.rb โ€” Seed data

spec/test/ โ€” Test code
lib/ โ€” Custom libraries
public/ โ€” Static files (error pages, etc.)

Key Points

1

app/models/ โ€” Data models and business logic (User, Post, Comment, etc.)

2

app/controllers/ โ€” HTTP request handling (PostsController, UsersController, etc.)

3

app/views/ โ€” HTML templates (ERB files, controller_name/action_name.html.erb)

4

config/routes.rb โ€” URL routing rules

5

db/migrate/ โ€” Database schema change history (timestamp_description.rb)

6

spec/ or test/ โ€” Test code (model specs, controller specs, etc.)

Pros

  • Clear separation by role
  • Predictable code locations
  • rails generate auto-creates files in correct locations
  • Consistent structure across Rails projects

Cons

  • Overwhelming number of directories at first
  • app/models/ etc. can bloat in large projects
  • Paths get long as namespace structure grows complex

Use Cases

Starting a new Rails project Determining file locations in team projects Understanding file roles during code review CI/CD pipeline configuration