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
Linux Directory Structure
Prerequisite: Linux File Hierarchy Structure In Linux/Unix operating system everything is a file even directories are files, files are files, and devices like mouse, keyboard, printer, etc are also files. Here we are going to see the Directory Structure in Linux. Types of files in the Linux system.
5 min read
Ruby on Rails - Database Setup
Ruby and Rails is a powerful web software framework that simplifies database control and interactions. right database setup is crucial for any Rails utility, as it guarantees efficient information garage and retrieval. This guide will walk you through putting in databases on your Rails application,
5 min read
Ruby on Rails Tutorial
Ruby on Rails or also known as rails is a server-side web application development framework that is written in the Ruby programming language, and it is developed by David Heinemeier Hansson under the MIT License. It supports MVC(model-view-controller) architecture that provides a default structure f
9 min read
Ruby on Rails Introduction
Ruby on Rails or also known as rails is a server-side web application development framework that is written in the Ruby programming language, and it is developed by David Heinemeier Hansson under the MIT License. It supports MVC(model-view-controller) architecture that provides a default structure f
6 min read
Django vs Ruby On Rails
When you're building a website or web application, picking the right framework can be a big decision. Two popular choices are Django and Ruby on Rails. Django uses Python, while Ruby on Rails uses Ruby. Each has its own way of doing things, and they both have their strengths. This article will break
7 min read
How to create API in Ruby on Rails?
Building APIs with Ruby on Rails: A Step-by-Step GuideRuby on Rails (Rails) is a popular framework for web development, known for its convention over configuration approach. It also excels in creating robust and maintainable APIs (Application Programming Interfaces). APIs act as intermediaries, allo
3 min read
How to Set Cookie in Ruby on Rails?
Cookies are small pieces of data that are sent from a website and stored on the user's computer by the user's web browser. They are commonly used for session management, user authentication, personalization, and tracking. In Ruby on Rails, you can use the 'cookies' object to set, read, and delete co
3 min read
Ruby on Rails - Controller
In Ruby on Rails, a Controller manages the flow of data between the Model and the View. When a user makes a request, the Controller decides what data to fetch or update, and which view to display. Think of it as the middleman that takes user input, processes it with the help of the Model, and sends
6 min read
Ruby on Rails Filters
In the web development landscape, efficiently managing request-response cycles is paramount. Ruby on Rails, a prominent web application framework, provides a powerful feature called "filters" within its MVC architecture. Filters enable developers to execute specific code at defined points during the
2 min read
Ruby Directories
A directory is a location where files can be stored. For Ruby, the Dir class and the FileUtils module manages directories and the File class handles the files. Double dot (..) refers to the parent directory for directories and single dot(.)refers to the directory itself.The Dir Class The Dir class p
5 min read