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 controllersapp/views/โ ERB templates (HTML)app/helpers/โ View helper methodsapp/mailers/โ Email deliveryapp/jobs/โ Background jobsapp/channels/โ Action Cable (WebSocket)app/javascript/โ JavaScript filesapp/assets/โ Images, fonts, and other static files
config/ โ Configuration files
routes.rbโ URL โ Controller mappingdatabase.ymlโ DB connection infocredentials.yml.encโ Encrypted secretsinitializers/โ Settings executed at Rails startup
db/ โ Database
migrate/โ Migration files (schema change history)schema.rbโ Current schema snapshotseeds.rbโ Seed data
spec/test/ โ Test code
lib/ โ Custom libraries
public/ โ Static files (error pages, etc.)
Key Points
app/models/ โ Data models and business logic (User, Post, Comment, etc.)
app/controllers/ โ HTTP request handling (PostsController, UsersController, etc.)
app/views/ โ HTML templates (ERB files, controller_name/action_name.html.erb)
config/routes.rb โ URL routing rules
db/migrate/ โ Database schema change history (timestamp_description.rb)
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