Ruby on Rails Directory Structure - Ruby on Rails

You need not provide any input when you use the Rails helper script to create your application. Since, Rails knows where to find things it needs within this structure, it creates the entire directory structure for the application.

Let us take a top-level view of a directory tree created by the helper script at the time of application creation. Barring the minor changes between releases, every Rails project will have the same structure, with the same naming conventions. Due to this consistency, you can quickly move between Rails projects without relearning the project's organization.

Let us recall the demo application while explaining the installation. It can be created using a simple helper command rails demo

Now, go into the demo application root directory as follows −

Directory

What are the directories and what are it's purposes?

  • app − It is the core directory and organizes your application components. It's got subdirectories that hold the MVC, ie the backend business logic (models), view (views and helpers) and controller (controllers).
  • app/controllers − Here is where Rails looks to find the controller classes. This subdirectory handles a web request from the user.
  • app/helpers − The helpers subdirectory holds all helper classes used to assist the MVC classes. This helps to keep the MVC code small, focused, and uncluttered.
  • app/models − It has the classes that model and wrap the data stored in our application's database. Models are very simple in Rails unlike most other frameworks.
  • app/view − It is here where the display templates to fill in with data from our application, convert to HTML, and return to the user's browser.
  • app/view/layouts − This subdirectory holds the template files for layouts to be used with views. This models the common header/footer method of wrapping views. In your views, define a layout using the <tt>layout:default</tt> and create a file named default.html.erb. Inside default.html.erb, call <% yield %> to render the view using this layout.
  • components − This directory holds components, tiny self-contained applications that bundle model, view, and controller.
  • config − This houses all the application's configuration files, including your database configuration (in database.yml), your Rails environment structure (environment.rb), and routing of incoming web requests (routes.rb). The behavior of the three Rails environments for test, development, and deployment with files found in the environments directory can also be customised.
  • db − This holds the database related files. The relational database with scripts you create and place in this directory can be managed here.
  • doc − Ruby automatically generate documentation for code you create using a framework called RubyDoc. This directory holds all the RubyDoc-generated Rails and application documentation.
  • lib − This is where you put libraries unless they explicitly belong elsewhere (such as vendor libraries).
  • log − This directory holds error logs. Rails automatically creates scripts that help you manage various error logs. Separate logs for the server (server.log) and each Rails environment (development.log, test.log, and production.log) can be found here.
  • public − This is where all web files that don't change, such as JavaScript files (public/javascripts), graphics (public/images), stylesheets (public/stylesheets), and HTML files (public) goes.
  • script − This directory holds scripts to launch and manage the various tools that you'll use with Rails such as scripts to generate code (generate) and launch the web server (server).
  • test − This directory contains the tests you write as well as the built in tests that Rails creates for you. There are subdirectories for mocks (mocks), unit tests (unit), fixtures (fixtures), and functional tests (functional).
  • tmp− Temporary files for intermediate processing are stored here.
  • vendor− This directory houses the third party vendor libraries such as security libraries or database utilities beyond the basic Rails distribution) go here.In addition to these directories, there will be two files available in demo directory.
  • README − It gives basic details about the Rail Application and description of the directory structure.
  • Rakefile − This file helps with building, packaging and testing the Rails code. This will be used by rake utility supplied along with the Ruby installation.

All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

Ruby on Rails Topics