Rails Folder Structure
Every Rails application has the same folder layout. Rails puts specific types of files in specific folders. Once you understand this structure, you can navigate any Rails project — even one written by someone else — without getting lost.
Think of the folder structure like the floor plan of a building. Every room has a name and a purpose.
The Full Folder Map
myblog/ | +-- app/ ← Your main application code | +-- controllers/ ← Handle requests from browsers | +-- models/ ← Talk to the database | +-- views/ ← HTML templates shown to users | +-- helpers/ ← Small reusable view functions | +-- mailers/ ← Email sending logic | +-- jobs/ ← Background task code | +-- assets/ ← Images, CSS, JavaScript | +-- config/ ← App settings and configuration | +-- routes.rb ← URL definitions | +-- database.yml ← Database connection settings | +-- environments/ ← Settings per environment | +-- db/ ← Database files | +-- migrate/ ← Database change history | +-- schema.rb ← Current database structure | +-- Gemfile ← List of gems (libraries) your app uses +-- Gemfile.lock ← Exact gem versions currently installed | +-- public/ ← Static files served directly +-- log/ ← Log files for debugging +-- tmp/ ← Temporary files +-- test/ ← Automated test files +-- vendor/ ← Third-party code (rarely used today)
The app/ Folder — Where You Spend Most of Your Time
The app/ folder holds the code you write every day. Each subfolder has a clear job.
app/controllers/
Controllers receive requests from the browser and decide what to do. They fetch data from models and send it to views.
Example: app/controllers/posts_controller.rb When user visits /posts → PostsController runs → fetches all posts → sends to view
app/models/
Models represent the data in your app. They communicate with the database and hold your business rules.
Example: app/models/post.rb Post.all → gets all posts from database Post.find(1) → gets the post with ID = 1 post.save → saves a post to the database
app/views/
Views hold your HTML templates. Rails uses a format called ERB (Embedded Ruby) that mixes HTML with Ruby code.
File naming convention: app/views/[controller_name]/[action_name].html.erb Example: app/views/posts/index.html.erb ← shown when PostsController#index runs app/views/posts/show.html.erb ← shown when PostsController#show runs
app/assets/
Assets include your CSS stylesheets, JavaScript files, and images. Rails processes these through its asset pipeline before serving them to browsers.
app/assets/ +-- stylesheets/ ← your CSS files +-- javascript/ ← your JS files +-- images/ ← your image files
The config/ Folder — App Settings
config/routes.rb
This file defines every URL your app responds to. When a browser sends a request, Rails checks this file first to figure out which controller and action should handle it.
config/database.yml
This file stores database connection settings for three environments:
development: ← the database you use while building test: ← a separate database used when running tests production: ← the database your live app uses
config/environments/
Three files here control how Rails behaves in each environment. In development, Rails shows detailed error messages. In production, it hides errors from users and caches aggressively for speed.
The db/ Folder — Database History
db/migrate/
Each file in this folder represents one change to your database structure — adding a table, adding a column, or removing a column. Rails applies these in order to build your database.
db/migrate/ 20240101_create_users.rb ← creates the users table 20240110_add_email_to_users.rb ← adds email column later
db/schema.rb
This file shows your current database structure in one place. It is auto-generated by Rails every time you run migrations. Never edit it by hand.
Gemfile and Gemfile.lock
The Gemfile lists all the external libraries (gems) your app uses. You add gems here and then run bundle install to download them.
Gemfile example: gem "rails", "~> 7.1.0" gem "devise" ← adds user authentication gem "pg" ← adds PostgreSQL support
The Gemfile.lock records the exact version of every gem that was installed. Commit this file to version control so your whole team uses the same versions.
The public/ Folder
Files in the public/ folder are served directly to browsers without going through Rails. The 404.html and 500.html pages live here. This is also where Rails compiles your assets for production.
Folder to File Naming Convention
| What You Create | File Location | File Name Pattern |
|---|---|---|
| A model named Post | app/models/ | post.rb |
| A controller for posts | app/controllers/ | posts_controller.rb |
| A view for listing posts | app/views/posts/ | index.html.erb |
| A migration to create posts | db/migrate/ | 20240101_create_posts.rb |
Rails uses these naming rules automatically. When you name things correctly, Rails connects them without any extra configuration. This is "Convention Over Configuration" in action.
Key Takeaway
You do not need to memorize every folder on day one. Start with app/controllers, app/models, app/views, and config/routes.rb. These four locations handle 80% of the code you write in a typical Rails application.
