How to Ruby on Rails- Directory Structure

This guide explains the default directory structure generated by Ruby on Rails. Let’s get started with the directory structure. In the last tutorial, we discussed the installation of Rails. The RailsInstaller already created an app named Todo in C:\\sites.

Directory Structure

To create a new Rails app, open up Rail command line and run the following command:

Syntax
     rails new sample_app

Rails will automatically create its own directories and files.

Result

Rails Directory structure

 

Once Rails created the app, You will find the directory structure as follows

Directory Structure
	.
	|-- app
	|   |-- assets
	|   |   |-- images
	|   |   |-- javascripts
	|   |   |   `-- application.js
	|   |   `-- stylesheets
	|   |       `-- application.css
	|   |-- controllers
	|   |   |-- application_controller.rb
	|   |   `-- concerns
	|   |-- helpers
	|   |   `-- application_helper.rb
	|   |-- mailers
	|   |-- models
	|   |   `-- concerns
	|   `-- views
	|       `-- layouts
	|           `-- application.html.erb
	|-- bin
	|   |-- bundle
	|   |-- rails
	|   |-- rake
	|   |-- setup
	|   `-- spring
	|-- config
	|   |-- application.rb
	|   |-- boot.rb
	|   |-- database.yml
	|   |-- environment.rb
	|   |-- environments
	|   |   |-- development.rb
	|   |   |-- production.rb
	|   |   `-- test.rb
	|   |-- initializers
	|   |   |-- assets.rb
	|   |   |-- backtrace_silencers.rb
	|   |   |-- cookies_serializer.rb
	|   |   |-- filter_parameter_logging.rb
	|   |   |-- inflections.rb
	|   |   |-- mime_types.rb
	|   |   |-- session_store.rb
	|   |   `-- wrap_parameters.rb
	|   |-- locales
	|   |   `-- en.yml
	|   |-- routes.rb
	|   `-- secrets.yml
	|-- config.ru
	|-- db
	|   `-- seeds.rb
	|-- Gemfile
	|-- Gemfile.lock
	|-- lib
	|   |-- assets
	|   `-- tasks
	|-- log
	|-- public
	|   |-- 404.html
	|   |-- 422.html
	|   |-- 500.html
	|   |-- favicon.ico
	|   `-- robots.txt
	|-- Rakefile
	|-- README.rdoc
	|-- test
	|   |-- controllers
	|   |-- fixtures
	|   |-- helpers
	|   |-- integration
	|   |-- mailers
	|   |-- models
	|   `-- test_helper.rb
	|-- tmp
	|   `-- cache
	|       `-- assets
	`-- vendor
	`-- assets
		|-- javascripts
		`-- stylesheets

 

Let’s go through each directory and files,

app: This organizes your application components. It’s got subdirectories that hold the view, controller, and the backend business logic.

app/assets: This contains the static files required for the application’s front-end grouped into folders based on their type.

app/controllers − The ‘controllers’ directory is where Rails looks to find controller classes. A controller handles a web request from the user. Controllers are responsible for orchestrating the model and views.

app/helpers: This is where all the helper functions for views reside. There are already a few pre-created helpers available.

app/mailers: The mailers directory contains the mail (as in “email”) specific functions for the application. Mailers are similar to controllers and they will have their view files in app/views/mailer_name/

app/models: All model files live in the app/models directory. Models act as object-relational mappers to the database tables that hold the data.

app/view: The third part of the MVC architecture are the views. The files related to the views go into this directory. The files are a combination of HTML and Ruby and are organized based on the controller to which they correspond.

app/view/layouts: This folder holds the layout for all your view files, which they inherit. Files created in here are available across all the view files.

bin: This directory contains Binstubs for the Rails application. Binstubs are nothing but wrappers to run the gem executables scoped to your application.

Config: As the name suggests this contains all the application’s configuration files. The database connection and application behavior can be altered by the files inside this directory.

db: Rails application will have model objects that access relational database tables. You can manage the relational database with scripts you create and place in this directory.

Gemfile: The Gemfile is the place where all your app’s gem dependencies are declared.

Gemfile.lock: Gemfile.lock holds the gem dependency tree, including all versions, for the app.

lib: lib directory is where all the application specific libraries reside.

log: This holds all the log files. Rails automatically create files for each environment.

public: The public directory contains some of the common files for web applications.

test: The folder name says it all. This holds all the test files for the app.

tmp: This is the temporary directory for the app to hold files like caches.

vendor/assets: This is where the vendor files go, such as javascript libraries and CSS files, among others.

Muhsin Yakkiparamban

Muhsin Yakkiparamban is the co-founder of Lauyou Learning. Muhsin holds Bachelor Degree in Civil Engineering from KMCT Calicut.

You may also like...