Rails App Structure
Overview
This a simple guide to the main files and folders in a Rails application. There might be slight differences depending on your Rails version, but the core structure is mostly the same.
test_rails_app/
├── Dockerfile
├── Gemfile
├── Gemfile.lock
├── README.md
├── Rakefile
├── app
├── bin
├── config
├── config.ru
├── db
├── lib
├── log
├── public
├── script
├── storage
├── test
├── tmp
└── vendor
Most of the work will be done in the app folder. Other folders like config and db are essential for setup and data, while root files manage dependencies and instructions.
App Folder
This is where most of the code lives.
-
Assets
- Stores static files like images and stylesheets
- CSS controls how the HTML elements look.
imagesfolder is for layout images, not user uploadsstylesheetscontains CSS files for styling viewsapplication.cssmakes all styles available to the views.
-
Channels
- Used for real-time features like chat or live notifications
- Connected to
application_cablefor broadcasting - Lets your app send messages to users in real time.
-
Controllers
- Houses all controller files
application_controller.rbhas default behavior for all controllers- Other controllers inherit from this file
- Inheritance allows shared functionality across all controllers.
-
Helpers
- Contains helper methods used in views only
- Makes view templates cleaner and easier to manage
-
JavaScript
- Managed with Webpack in Rails 6+
packs/application.jsis the main JavaScript file- Linked in the layout via
javascript_pack_tag - Ensures JavaScript code is available throughout the app.
-
Models
- Models handle data and database interactions.
application_record.rbis the base class for all models- New models inherit from this base
-
Views
- Views define the HTML structure
- Contains templates for what users see
- Links all CSS and JavaScript automatically
layouts/application.html.erbis the main layout file- All other views render inside this layout using
yield
Bin Folder
This folder is mostly handled by Rails and usually doesn’t require changes.
- Stores executable scripts
- Not commonly used in most app development
Config Folder
Configurations help your app behave correctly in different environments.
- Stores configuration for the app
environments/defines settings for development, test, and productioncredentials.yml.encholds API keys and secretsroutes.rbdefines URL routes for the application
DB Folder
This folder holds development and test databases (usually SQLite by default).
- Migration files define database tables
schema.rbshows the structure of all tables
Think of tables like Excel spreadsheets with rows and columns for storing data.
Root Files
These root files manage dependencies, documentation, and project settings.
-
Gemfile
- Lists Ruby gems your app uses
- Update this to add or remove gems
Gemfile.lockstores locked versions, not edited directly
-
package.json
- Lists JavaScript dependencies installed via Yarn
-
README.md
- Markdown file for instructions or documentation
- Displayed on GitHub or other code repositories
-
Hidden files
- Start with a dot, e.g.,
.gitignoreor.ruby-version - Not shown by default in editors
- Start with a dot, e.g.,