DEV Community

Samuel Grasse-Haroldsen
Samuel Grasse-Haroldsen

Posted on • Updated on

Rails Directory Structure

When you run rails new app a whole lot of files are generated. Thankfully they are well organized, and we can easily find/create the files we need. I wanted to write about some of the more important directories and what they contain.

app

Just like the name suggests, this directory holds the components of our app.

  • assets: files that need to be preprocessed ex) CoffeeScript
  • channels: the 'real-time' equivalent of controllers (more about integrating web sockets in Rails to come)
  • controllers: where our app determines what to do with web requests
  • helpers: any complicated code that could potentially make our other MVC components messy
  • javascript: the muscle of our views (interactivity)
  • jobs: any processes we need to run in parallel outside the normal MVC design (mailings/billings/etc.)
  • mailers: controllers for mail. The views of these mail controllers live in the views directory
  • models: where we encapsulate (model) our data in classes
  • views: the web pages our controllers serve up as html to the client

bin

This directory simply contains wrappers for gem executables. This simply means that we can run commands such as rails generate with ./bin/rails generate. We why would want to do this is because of versioning. If you have the latest version of rails on your machine but you are working on a project with an older version of rails, you could run into trouble. Check out this stackoverflow answer for more information. By default these "binstubs" are generated:

  • bundle
  • rails
  • rake
  • setup
  • spring
  • webpack
  • webpack-dev-server
  • yarn

config

This contains, you guessed it, configuration files! Most notably among them include:

  • database.yml: the configuration of your database(s)
  • environment.rb: this is where our rails app is initialized
  • routes.rb: where all our different routes live

db

Where we manage our database. Migrations, seed files, and schema all live here!

  • migrate: our migrations (db alterations) can be found here
  • schema.rb: an auto-generated file that reflects the current state of our database
  • seeds.rb: our script to rapidly fill our db with data (useful for development)

lib

The home of extended modules or code that isn't app specific (doesn't fall under the categories of anything in the app file).

log

Where errors are logged.

public

Similar to app/assets this is where static assets are located. The main difference between the two locations is clearly stated in the rails guide:

"Any assets under public will be served as static files by the application or web server when config.serve_static_files is set to true. You should use app/assets for files that must undergo some preprocessing before they are served."

storage

This directory is specifically for Active Storage which is a way to upload files to cloud services such as Amazon S3, Google Cloud Storage, and Microsoft Azure Storage. You can do a lot more with Active Storage! I'll write more about this later as well.

test

I think you already know this one, so instead of telling you what is in here, I am going to reiterate the value of writing tests for you applications and the power of TDD!

tmp

Temporary files such as cache and pid files are stored here. Make sure you don't remove this directory from your .gitignore (made that mistake once and wow does it contain a lot of stuff!)

vendor

You can find third party code here. Check out this article to learn more about the subtle differences between lib and vendor.

Gemfile and Gemfile.lock

Although not directories, these files describe our gem dependencies. Gemfile is our app's list of dependencies. Gemfile.lock is the automatic list of gems and their versions that were installed by bundler (bundle install).

Top comments (0)