Rails Directory Structure: What Goes in app/, config/ and db/
A walk through the folders rails new creates
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