Where Things Go

Let’s look in depth at Ruby's directory structure.

Rails assumes a certain runtime directory layout and provides application and scaffold generators, which will create this layout for us. For example, if we generate my_app using the command rails new my_app, the top-level directory for our new application appears as shown in the figure below.

Let’s start with the text files in the top of the application directory:

  • The config.ru file configures the Rack Webserver Interface to either create Rails Metal applications or to use Rack Middlewares in our Rails application. These are discussed further in the Rails Guides.

  • The Gemfile file specifies the dependencies of our Rails application. We’ve already seen this in use when the bcrypt-ruby gem was added to the Depot application. Application dependencies also include the database, web server, and even scripts used for deployment. Technically, this file isn’t used by Rails, but rather by our application. We can find calls to the Bundler in the config/application.rb and config/boot.rb files.

  • The Gemfile.lock file records the specific versions for each of our Rails application’s dependencies. This file is maintained by Bundler and should be checked into our repository.

  • The Rakefile file defines tasks to run tests, create documentation, extract the current structure of our schema, and more. Type rake -T at a prompt for the full list. Type rake -D task to see a more complete description of a specific task.

  • The README file contains general information about the Rails framework.

Let’s look at what goes into each directory (in no particular order).

A place for our application

Most of our work takes place in the app directory. The main code for the application lives below the app directory, as shown in figure below. We’ll talk more about the structure of the app directory as we look at the various Rails modules such as Active Record, Action Controller, and Action View in more detail later in the course.

A place for our tests

As we have seen in Unit Testing of Models, Functional Testing of ...