A guide to understanding Ruby on Rails directory structure
Switch branches/tags
Nothing to show
Clone or download
Latest commit dcdada3 Oct 16, 2017
Permalink
Failed to load latest commit information.
README.md Added workers Oct 15, 2017

README.md

Rails Directory Structure Guide

There is a default directory structure that Ruby on Rails comes with. The Rails guides go in depth in explaining the folder structure, but does not talk about the extra folders that commonly get added in Rails Apps.

The goal of this guide is to be a central place to learn about ALL the folders that are used in Rails apps, not just the ones that come by default (Marked with asterisk).

We will also go over explaining RSpec folder structure and any additional folders.

Directory Structure


app

It organizes your application components. It's got subdirectories that hold the views, controllers, models, and more that handle business logic.

app/assets

Holds the assets for your application including images, stylesheets, and javascript.

*app/cells

App cells are simple ruby classes that can render templates.

  • github Trailblazer framework
  • video RailsConf talk about trailblazer.

app/channels

Contains channels used to setup connections with ActionCable.

app/controllers

Contains app controllers.

*app/decorators

App decorators is a design pattern to remove view methods from models.

  • github draper gem
  • article explanation of decorator pattern.
  • video Railscast - using draper gem.
  • video RailsConf - decorators.

The Draper Gem is a popular choice to use to help with creating and using decorators.

*app/forms

Form Object is a design pattern that encapsulates logic related to validating and persisting data. Using these can improve the way you implement complex forms.

  • article about using form objects in rails.
  • video Railscast - implementing form objects.

app/helpers

Contains app helpers.

*app/inputs

Simple form allows you to create custom input components and you can place them here.

  • github simple form gem.
  • github create custom input component.

app/jobs

Contains app jobs.

app/mailers

Contains app mailers.

app/models

Contains app models.

*app/performers

Performers is another design pattern to abstract view methods from the model using modules.

  • github details on the Performer Pattern.

*app/policies

Policies are plain old ruby objects that handle presentation logic.

*app/presenters

Presenters is another design pattern to abstract view methods from the model using PORO.

  • article about using presenters in rails.
  • article comparing presenters to decorators.
  • video Railscast - implementing a presenter.

*app/services

Contains app services. A service object implements the user’s interactions with the application. It contains business logic that describe the connections with your domain objects.

  • article about using services in rails.
  • video Railscast - implementing a service.
  • video domain driven Rails

*app/uploaders

A uploader is a class that is used by CarrierWave gem to model an uploaded file.

  • github CarrierWave gem.
  • video Railscast - file uploading with CarrierWave.

*app/use_cases

Use Cases is pretty much the same thing as services. They are designed to break up non-trivial business logic.

*app/values

Value objects are an abstraction where equality is based on internal fields instead of identity.

  • github ValueObjects gem
  • article discussion and examples.
  • video lecture on value objects.

app/views

Contains app views.

*app/workers

Workers are objects that allow you to run processes in the background. Remeber, it is recommended to use active job instead of your own workers so you can later switch out job runners without having to worry about api differences.


bin

Contains the rails script that starts your app and can contain other scripts you use to setup, update, deploy or run your application.


config

Configure your application's routes, database, and more. This is covered in more detail in Configuring Rails Applications.


db

Contains your current database schema, as well as the database migrations.


lib

Extended modules for your application.


log

Application log files.


public

The only folder seen by the world as-is. Contains static files and compiled assets.


test

Unit tests, fixtures, and other test apparatus. These are covered in Testing Rails Applications.


spec

Alternative to test directory using BDD. Rspec allows you to write an alternative syntax to Test Unit that reads more like a specification than a test.


tmp

Temporary files (like cache and pid files).


vendor

A place for all third-party code. In a typical Rails application this includes vendored gems.