Ruby on Rails - Directory Structure
Last Updated :
27 Aug, 2024
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 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
- assets: Asset directory is responsible for managing static assets like stylesheets(external css files), JavaScript files, and images(.png, .jpg formats)
- controllers: Controllers directory contains the controllers that handles the incoming requests and generate outgoing responses.
- models: The models directory houses the structure of the application's data model.
- 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.
Similar Reads
Non-linear Components
In electrical circuits, Non-linear Components are electronic devices that need an external power source to operate actively. Non-Linear Components are those that are changed with respect to the voltage and current. Elements that do not follow ohm's law are called Non-linear Components. Non-linear Co
11 min read
Class Diagram | Unified Modeling Language (UML)
A UML class diagram is a visual tool that represents the structure of a system by showing its classes, attributes, methods, and the relationships between them. It helps everyone involved in a projectâlike developers and designersâunderstand how the system is organized and how its components interact
12 min read
Spring Boot Tutorial
Spring Boot is a Java framework that makes it easier to create and run Java applications. It simplifies the configuration and setup process, allowing developers to focus more on writing code for their applications. This Spring Boot Tutorial is a comprehensive guide that covers both basic and advance
10 min read
AVL Tree Data Structure
An AVL tree defined as a self-balancing Binary Search Tree (BST) where the difference between heights of left and right subtrees for any node cannot be more than one. The absolute difference between the heights of the left subtree and the right subtree for any node is known as the balance factor of
4 min read
Backpropagation in Neural Network
Backpropagation is also known as "Backward Propagation of Errors" and it is a method used to train neural network . Its goal is to reduce the difference between the modelâs predicted output and the actual output by adjusting the weights and biases in the network. In this article we will explore what
10 min read
What is Vacuum Circuit Breaker?
A vacuum circuit breaker is a type of breaker that utilizes a vacuum as the medium to extinguish electrical arcs. Within this circuit breaker, there is a vacuum interrupter that houses the stationary and mobile contacts in a permanently sealed enclosure. When the contacts are separated in a high vac
13 min read
3-Phase Inverter
An inverter is a fundamental electrical device designed primarily for the conversion of direct current into alternating current . This versatile device , also known as a variable frequency drive , plays a vital role in a wide range of applications , including variable frequency drives and high power
13 min read
Polymorphism in Java
Polymorphism in Java is one of the core concepts in object-oriented programming (OOP) that allows objects to behave differently based on their specific class type. The word polymorphism means having many forms, and it comes from the Greek words poly (many) and morph (forms), this means one entity ca
7 min read
Random Forest Algorithm in Machine Learning
A Random Forest is a collection of decision trees that work together to make predictions. In this article, we'll explain how the Random Forest algorithm works and how to use it. Understanding Intuition for Random Forest AlgorithmRandom Forest algorithm is a powerful tree learning technique in Machin
7 min read
Spring Boot Interview Questions and Answers
Spring Boot is a Java-based framework used to develop stand-alone, production-ready applications with minimal configuration. Introduced by Pivotal in 2014, it simplifies the development of Spring applications by offering embedded servers, auto-configuration, and fast startup. Many top companies, inc
15+ min read