In a Rails 8.1 app, what does each folder under app/ hold, and which base classes does rails new put there?
answer
- folders by role, not by feature
- Application* base class per framework
- ApplicationRecord with primary_abstract_class
- app/javascript from the installers
- views/layouts and views/pwa
basics
~20 sRails 8.1's app/ is split by role: models, controllers, views, helpers, mailers, jobs, assets and javascript. rails new seeds each Ruby folder with an Application* base class, such as ApplicationRecord and ApplicationController, that the app's own classes inherit from.
solid answer
~30 sUnder `app/` each folder holds one kind of object: `models/` (Active Record and plain domain classes), `controllers/`, `views/` (templates and layouts), `helpers/` (view helper modules), `mailers/`, `jobs/`, `assets/` (stylesheets and images served by Propshaft) and `javascript/` (import-map entry point and Stimulus controllers). `rails new` writes one base class per framework: `ApplicationRecord < ActiveRecord::Base` marked `primary_abstract_class`, `ApplicationController < ActionController::Base` with `allow_browser versions: :modern`, `ApplicationJob`, `ApplicationMailer` with `layout "mailer"`, and the `ApplicationHelper` module. Your classes inherit from these, so app-wide behaviour goes in one place. It also writes `views/layouts/application.html.erb`, the mailer layouts and the PWA files in `views/pwa`, plus empty `controllers/concerns` and `models/concerns`.
code
ruby · 10 lines# app/models/application_record.rb
class ApplicationRecord < ActiveRecord::Base
primary_abstract_class
end
# app/controllers/application_controller.rb
class ApplicationController < ActionController::Base
# Only allow modern browsers supporting webp images, web push, badges, import maps, CSS nesting, and CSS :has.
allow_browser versions: :modern
endgo deeper
Recall each app/ folder and its role, and that every model, controller, job and mailer inherits from a generated Application* parent.
Explain why the base classes exist, what primary_abstract_class does for ApplicationRecord, and how skipped frameworks remove their folders.
Judge when a new role folder such as app/services earns its place versus keeping plain classes in app/models, and keep the layout predictable for newcomers.
Set conventions for how app/ grows across teams so that ten developers still find code where the layout promises it.
## Organised by role A Rails app keeps almost all of its own code under **`app/`**, and the first level of that folder is organised by **role**, not by feature. A blog's post-related code is spread across `app/models/post.rb`, `app/controllers/posts_controller.rb` and `app/views/posts/`, and every developer who has seen one Rails app knows where to look in the next one. That predictability is the point of the layout. ## The folders a Rails 8.1 app starts with | Folder | Holds | Seeded with | |---|---|---| | `app/models` | Active Record models and plain domain classes | `application_record.rb`, `concerns/` | | `app/controllers` | controllers that handle requests | `application_controller.rb`, `concerns/` | | `app/views` | templates, partials, layouts | `layouts/application.html.erb`, `layouts/mailer.*.erb`, `pwa/` | | `app/helpers` | modules of methods for views | `application_helper.rb` | | `app/mailers` | Action Mailer classes | `application_mailer.rb` | | `app/jobs` | Active Job classes | `application_job.rb` | | `app/assets` | stylesheets and images for Propshaft | `stylesheets/application.css`, `images/` | | `app/javascript` | JavaScript entry point and Stimulus controllers | written by `importmap:install` and `stimulus:install` | Skipping a framework removes its folder: `--skip-active-job` drops `app/jobs`, `--skip-action-mailer` drops `app/mailers` and the mailer layouts. ## The base classes and why they exist `rails new` writes one abstract parent per framework so that app-wide behaviour has a single home: - **`ApplicationRecord < ActiveRecord::Base`**, marked with `primary_abstract_class`, so it has no table of its own and every model inherits its connection settings. - **`ApplicationController < ActionController::Base`**, which in 8.1 starts with `allow_browser versions: :modern` and, in import-map apps, `stale_when_importmap_changes`. - **`ApplicationJob < ActiveJob::Base`**, where retry and discard rules shared by every job can go. - **`ApplicationMailer < ActionMailer::Base`**, with `default from: "[email protected]"` and `layout "mailer"`. - **`ApplicationHelper`**, a module rather than a class, for helpers every view can use. Generators follow the same convention: `bin/rails generate model Order` writes `class Order < ApplicationRecord`, not `< ActiveRecord::Base`. ## Folders that appear later - `app/channels` arrives when you generate an Action Cable channel; a new 8.1 app does not have one. - `app/javascript/controllers` holds Stimulus controllers; `stimulus:install` seeds it with `application.js`, `index.js` and `hello_controller.js`. - Folders you create yourself, such as `app/services` or `app/pdfs`, sit beside the generated ones. Rails picks up every subfolder of `app/` that exists at boot (except `assets`, `javascript` and `views`) as a root for constant lookup, so a new folder needs one server restart and no configuration. - The `concerns/` folders exist from the start, empty, as the home for shared modules. ## What an interviewer listens for 1. **Role-based placement**: a candidate who says "the controller goes in app/controllers, its templates in app/views/orders" has the model of the layout. 2. **Base classes**: knowing that models inherit from `ApplicationRecord` and controllers from `ApplicationController`, and why that indirection exists. 3. **Not everything is a model**: `app/models` may hold plain Ruby classes that are not tables, and adding a role folder is normal when one kind of object multiplies. ## Where things do not go Several habits from older Rails versions or other stacks put files in the wrong place: - **JavaScript in `app/assets/javascripts`**: that was the Sprockets layout. A Rails 8.1 import-map app keeps its JavaScript in `app/javascript`, and Propshaft does not compile JavaScript. - **Hand edits in build output**: `app/assets/builds` appears when a CSS or JS bundler is installed (for example by `tailwindcss-rails` or `cssbundling-rails`); its contents are build output and are ignored by git. - **Templates beside their classes**: a mailer's or controller's templates live under `app/views/<name>/`, not next to the Ruby file. - **Shared view code in controllers**: formatting methods for templates belong in `app/helpers`, which every view can call, rather than in controller methods exposed to views.
- Why do models inherit from `ApplicationRecord` instead of `ActiveRecord::Base` directly?`ApplicationRecord` is the app's own abstract base, marked `primary_abstract_class`, so behaviour every model should share, such as a common scope or connection settings, can live in one class without monkey-patching `ActiveRecord::Base`. Gems and engines that also subclass `ActiveRecord::Base` are not affected by what the app adds there.
- Where do a model's templates go if it has no controller, such as a mailer for orders?Mailer templates live under `app/views` too, in a folder named after the mailer, such as `app/views/order_mailer/confirmation.html.erb`, rendered inside `app/views/layouts/mailer.html.erb`. The mailer class itself sits in `app/mailers`, inheriting from `ApplicationMailer`.
- app/models may only contain Active Record classes backed by tables.
- Generated models inherit directly from ActiveRecord::Base.
- A new Rails 8.1 app has an app/channels folder by default.
- JavaScript files go in app/assets/javascripts for the asset pipeline.
- Helpers are classes that controllers instantiate per request.