Rails 4 structure

Rails 4 Directory Structure

When you generate a new Rails 4 application, it creates an entire directory structure together with what seems at first sight to be a bewildering collection of files. Some of you who are new to Rails may be wondering what all those generated files and directories are doing.

Rails generates an application by adhering to two great principles: It emphasizes convention-over-configuration, meaning it makes intelligent choices about what it thinks you want, and it organizes code by adopting the Model-View-Controller (MVC) architectural pattern. 

This short blog post gives an overview of the most important folders of a new Rails 4 application and describes the purpose of some key folders and files. First of all open your terminal or iTerm2 in my case and check Ruby and Rails version installed on your system.

Check Ruby Version

  
  ruby -v
    => ruby 2.3.1
  

Check Rails Version

  
  rails -v
    => Rails 4.2.6
  

I like to to use MySQL database instead of default SQLite which is not intended to be used as a production grade database.

Check MySQL Version

  
  mysql --version
    => mysql  Ver 14.14 Distrib 5.7.13, for osx10.11 (x86_64) using  EditLine wrapper
  

Now is time to generate new Rails 4 app.

Generate new Rails app with MySQL database

  
  rails new sample_app -d mysql
  

The above command generates the directory structure, which I describe below.

  • app directory is a place where you’ll spend most of your time and is the core directory of your entire app. Rails puts a lot of emphasis on keeping code organized, so the app directory has a number of sub-directories: 
    • assets sub-directory contains sub-directories where you’ll store your application’s images, JavaScript files, and stylesheet files required for the application’s front-end.
    • controllers sub-directory contains the controllers, which are Ruby files that handle the requests from the users. Controllers are responsible for orchestrating the interaction between models and views.
    • helpers sub-directory is used to take care of logic that is needed in the views. It is a great way to keep the views clean of logic and reuse that logic in multiple views.
    • mailers sub-directory contains the mail specific functions for your app. Mailers will have view files the same as controllers do.
    • models directory contains ruby files defining Classes that map each model to a database table. This is called Object Relational Mapping, and it is implemented by the Active Record Layer of Rails. If the database has a table called Users, the model (models/user.rb) will define a Ruby Class called User.
    • views sub-directory stores templates that will be displayed to the user after a successful request.

 

  • bin directory contains executables which is something you do not want to touch.

 

  • config as the name suggest has all the necessary configuration files of your Rails 4 application.
    • database.yml file holds all the database configuration. Different configurations can be set for different environments.(development, test, production)
    • routes.rb is a key Rails file. It maps incoming requests (URLs) to application code. Routing in Rails takes the incoming URL and picks it apart, directing the request to the appropriate controller and calling an action (method) defined in that controller.
    • environments sub-directory contains 3 files which define the settings for each environment (development, test, production).
    • initializers sub-directory contains the list of Ruby files that need to be run during the Rails initialization process.
    • secrets.yml file is a common storage location for the keys and credentials for various services such as Twitter, AWS, Stripe. I highly recommend adding this file to your .gitignore file to avoid accidently publishing your keys. Make sure the secrets in this file are kept private!

 

  • db directory stores everything related to the database.
    • migrate sub-directory contains all the migrations you create.
    • schema.rb file is a representation of your database generated by Rails when run the “rake db:migrate” command.
    • seeds.rb file is used to prefill database with records. Faker gem is excellent library to use.

 

  • lib directory is a place to store any reusable ‘library’ code that doesn’t belong to model, view or controller. It is something like an application specific gem.
    • assets sub-directory is a place for all the library assets such as scripts, stylesheets, images that are not application specific.
    • tasks sub-directory holds all application Rake tasks and other tasks can be put in this directory.

 

  • log directory is where Rails automatically creates log files so you have a record of what happened when your app ran. Each environment gets its own log file. The whole log folder should be added to your .gitignore file as there is no need to track its changes.

 

  • public directory is the document root directory of the app. It contains static files that are served directly by the web server. For example, 404.html lives here and gets served when a page can’t be found.

 

  • test directory is where you put test files if you use the default test library, organized further in the models, controllers, integration, helpers and mailers directories.

 

  • tmp directory is where Rails stores any temporary files needed by the app.

 

  • vendor directory is where we used to put third-party code. These days we generally use gems declared in the Gemfile instead.

 

  • Gemfile is a key Rails file. This file contains a list of third-party dependencies (Ruby gems) that your application needs.

 

  • Gemfile.lock file contains a list of third-party dependencies (Ruby gems) that your application needs.This file is generated when you run “bundle install” command