Open In App

Ruby on Rails - Directory Structure

Last Updated : 27 Aug, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

Ruby on Rails (often just called Rails) is a popular web application framework written in Ruby. It follows the convention over configuration principle, which means that it provides a lot of built-in conventions to simplify development. Understanding the Rails directory structure is key to effectively working with Rails applications. This article focuses on discussing directory structure in Ruby on Rails.

Directory Structure

In Ruby on Rails, the directory structure is organized to support the MVC (Model-View-Controller) architecture and various other aspects of web application development.

Below is an example screenshot of the directory structure in the Ruby on Rails application.

Directory Structure
Directory Structure

Folders and Files

A directory usually contains files and folders. Here is an overview of the files and folders inside the directory:

1. "app" directory

Majority of the application code resides in this directory. It contains many sub-directories that handle different functionalities of the application created. The few essential subdirectories within this app directory are

  1. assets: Asset directory is responsible for managing static assets like stylesheets(external css files), JavaScript files, and images(.png, .jpg formats)
  2. controllers: Controllers directory contains the controllers that handles the incoming requests and generate outgoing responses.
  3. models: The models directory houses the structure of the application's data model.
  4. views: Views directory contains the presentation layer which shows the virtual representation of data .

2. "bin" directory

Bin directory stands for "binary" that it includes basic binaries whichare required for the system's basic function. It contains many commands, scripts and more number of executable files

3. "config" directory

This directory contains configuration files of the application. It includes the files like routes.rb (defines the application's routes), database.yml (specifies connection details of the DB), and application.rb (configures the application-level settings).

4. "db" directory

Main tasks of this directory is managing database-related tasks. It contains the files to define a database schema (schema.rb), create and run database migrations (migrate directory), and seeds the database with initial data (seeds.rb).

5. "lib" directory

This directory is the home for custom libraries and modules that are specific to the application. Reusable code, extensions, or utility classes can be placed within this directory.

6. "log" directory

The log directory generates the log files which are software generated files containing information about the operations, activities, and usage patterns of an application, server.

7. "public" directory

It stores the static files that are served directly by the web server. Files like favicon.ico, robots.txt, or error pages resides within this directory.

8. "test" directory

The test cases for the rails application is written in this directory. Subdirectories like models, controllers, and integration are included in this directory to organize different types of tests.

9. "tmp" directory

The tmp directory holds the files temporarily for intermediate processsing.

10. "vendor" directory

The vendor directory contains the outside entities (assets), such as CSS frameworks or JS libraries.

11. "gemfile"

This directory is located on the root of the rails applications and it is used for describing gem dependencies for the Ruby program.

12. "gemfile.lock"

This is a gem repository that ensures a fresh checkout of using the exact same set of dependencies every time.

13. "Rakefile"

Rakefile is similar to Unix Makefile, which helps in building, packaging and testing the Rails code. This will be used by rake utility which is supplied along the Ruby installation.

14. "Readme.md"

This file is a common way to document the contents and structure of a folder so that any researcher can locate the information they need.

15. "components" directory

Components are theh modular parts of the rails application. However, there's no default components in a Rails directory. If the directory contains a component in it, it is custom made to organize the reusable parts of the project such as Rails engine, view components, service objects.

16. "doc" directory

It is used for storing documentation related to the project. It can include README files, API documentations. Rails encourages to keep the documentation close to the code.

17. "script" directory

Sripts were used in older versions of Rails for running the application such as rails console, rails server, rails generate, etc. In the later Rails application(from Rails 5) , these scripts have been moved to the bin, and the script is less commonly used.

Related Posts:

Conclusion

The directory structure in Rails provides a well-defined organized, top-level directories such as app, config, db and test. These directories serve for specific purposes and play a vital roles in managing different aspects of the application. By customizing the directory structure, it allows us to mold it for specific project requirements, such as organizing controllers into nested directories based on namespaces or adding custom directories for specific modules or features. Balancing between customization of the directory structure and following the rails conventions to maintain code readability, collaboration, and future scalability is a crucial one.


Next Article
Article Tags :

Similar Reads