Skip to content
Menu
Railshero
  • Blog
  • About Me
  • Contact Me
  • Privacy Policy
Railshero
raislhero.pw files and directories

Rails Basics: Files & Directories of The Rails Framework

Posted on June 27, 2023 by Rails Hero

Rails is a robust, flexible, and beginner-friendly framework for creating web applications. It follows the MVC (Model-View-Controller) design pattern, which helps keep the application organized, modular, and maintainable. Let’s delve into the intricacies of the Rails directory structure, explaining the purpose of each folder and file.

Ruby on Rails: File and Directory Structure at a Glance

Once you generate a new Rails application using the rails new command, you will notice a number of directories and files created by default. The fundamental structure looks like this:

app/
bin/
config/
db/
lib/
log/
public/
test/
tmp/
vendor/
.gitignore
.ruby-version
Gemfile
Gemfile.lock
Rakefile
README.md
config.ru

Let’s unpack each of these directories and files in detail.

The app/ Directory

The app/ directory is where you will spend most of your time when developing a Rails application. It’s the heart of any Rails application, housing the MVC components (models, views, controllers), helpers, mailers, jobs, and assets. Here’s a breakdown of its subdirectories:

  • models/: Contains files that define the data structure, validations, and logic of your application. Models interact with the database and encapsulate business logic.
  • views/: Stores files that determine what the end-user sees and interacts with. In Rails, views are primarily ERB (Embedded Ruby) files, but you might also see other templating systems like Haml.
  • controllers/: Contains files that act as the middleman between models and views. They handle user input, interact with models, and render appropriate views.
  • helpers/: Holds modules with methods to be used in views. They help keep views clean by moving complex logic out of them.
  • assets/: Contains images, stylesheets (CSS/SCSS), and JavaScript files. This directory further divides into images/, javascripts/, and stylesheets/ folders.
  • mailers/: Stores mailer classes, which are used to send emails to users.
  • channels/: Used for Action Cable, Rails’ real-time WebSockets feature. It includes files for setting up real-time features, like chat.
  • jobs/: Holds files for background jobs, which are tasks to be run outside the regular flow of the program.

The bin/ Directory

The bin/ directory contains scripts that help setup, update, and run the application. Some of the important ones are:

  • bundle: Ensures that the correct version of Bundler is running.
  • rails: Lets you interact with your application from the command line.
  • rake: Allows running rake tasks, which are scripts that automate certain aspects of development.
  • yarn: Facilitates JavaScript package management.
  • setup: Provides an easy way to get the application ready for development after cloning.

The config/ Directory

The config/ directory contains configuration files for your application, its routes, databases, and more. It is divided into several subdirectories and files:

  • environments/: Stores configuration files for different environments (development, test, production).
  • initializers/: Contains Ruby files that are run when the application starts.
  • locales/: Contains localization files to internationalize your application.
  • database.yml: Specifies the adapter and database configurations for different environments.
  • routes.rb: Where all the application routes are defined.
  • credentials.yml.enc: Contains encrypted application credentials.
  • master.key or credentials.yml.key: The key to decrypt credentials.yml.enc.
  • puma.rb: Contains Puma server’s configuration, which is the default web server for Rails.
  • boot.rb, application.rb, environment.rb, and spring.rb: Are involved in setting up the load paths, initializing the application and setting up specific settings for the Rails environment.

The db/ Directory

The db/ directory contains files related to your database:

  • migrate/: Holds migration files, which allow you to alter your database schema over time.
  • schema.rb or structure.sql: Represents the current state of your database schema.
  • seeds.rb: A file where you can put in sample data to populate your database during setup.

The lib/ Directory

The lib/ directory holds your own libraries/modules, tasks (added to Rake’s default set of tasks), and extensions that are not specific to Rails. This includes code that might be used by various applications or code that patches Ruby or Rails’ functionality.

The log/ Directory

The log/ directory contains log files generated by Rails for different environments. These files are invaluable for debugging.

The public/ Directory

The public/ directory holds static files and compiled assets. It includes 404.html and 500.html error pages, robots.txt, and favicon.

The test/ Directory

The test/ directory is where your tests reside, such as unit tests, fixtures, integration tests, etc. This is auto-generated if you use Rails’ default test suite, MiniTest.

The tmp/ Directory

The tmp/ directory contains temporary files like cache, pid, and session files.

The vendor/ Directory

The vendor/ directory is generally used for third-party code. In a typical Rails application, this might include vendored gems.

The .gitignore File

The .gitignore file specifies intentionally untracked files to ignore when using Git.

The .ruby-version File

The .ruby-version file specifies the version of Ruby that your application uses.

The Gemfile and Gemfile.lock Files

The Gemfile specifies the gem dependencies required by the application, and Gemfile.lock is a snapshot of all the exact gem versions used in the application.

The Rakefile File

The Rakefile file is where you can define tasks that can be run in the command line.

The README.md File

The README.md file is a documentation file where you explain what your application does, how to set it up, and any other information that users or other developers may find useful.

The config.ru File

The config.ru file is a Rack configuration file, used by Rack-based servers to start the application.


In conclusion, understanding the Rails directory structure can significantly improve your productivity and comfort level with Rails. Each file and folder in the structure has a purpose and knowing them can streamline your development process. The Rails framework is built around conventions, so once you become familiar with these directories and files, you will feel right at home in any Rails application.

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Tags

active record active storage assets assign to many associations attachment attachments authentication authorization avatar bootstrap cdn config database deploy deployments devise DRY environment variables file uploads Gemfile gems has_many helpers heroku index indexing initializer javascript pagination parameters postgres production public routes.rb ruby gem ruby gems search sendgrid server smtp stylesheets variants views voting

Recent Posts

  • Understanding the DRY Principle in Rails
  • Building Multi-tenant Applications with Rails
  • Rails Basics: Templating Engines
  • Deploying With Capistrano
  • Automated Testing in Rails

Archives

  • July 2023
  • June 2023
  • October 2021
  • September 2021
  • August 2021
  • July 2021

Categories

  • Active Record
  • Activity Logging
  • Apps
  • Assets
  • Attachments
  • Audio
  • Authentication
  • Authorization
  • Deployments
  • Error Pages
  • File Uploads
  • General
  • Heroku
  • Heroku
  • Pagination
  • Rails Basics
  • RubyGems
  • Search Engine Optimization
  • Search/Indexing
  • Testing
  • User Interface
  • Video
  • Views & Templating
  • Voting
  • Web Security
©2025 Railshero | Theme: Wordly by SuperbThemes
We use cookies on our website to give you the most relevant experience by remembering your preferences and repeat visits. By clicking “Accept All”, you consent to the use of ALL the cookies. However, you may visit "Cookie Settings" to provide a controlled consent.
Cookie SettingsAccept All
Manage consent

Privacy Overview

This website uses cookies to improve your experience while you navigate through the website. Out of these, the cookies that are categorized as necessary are stored on your browser as they are essential for the working of basic functionalities of the website. We also use third-party cookies that help us analyze and understand how you use this website. These cookies will be stored in your browser only with your consent. You also have the option to opt-out of these cookies. But opting out of some of these cookies may affect your browsing experience.
Necessary
Always Enabled
Necessary cookies are absolutely essential for the website to function properly. This category only includes cookies that ensures basic functionalities and security features of the website. These cookies do not store any personal information.
Non-necessary
Any cookies that may not be particularly necessary for the website to function and is used specifically to collect user personal data via analytics, ads, other embedded contents are termed as non-necessary cookies. It is mandatory to procure user consent prior to running these cookies on your website.
SAVE & ACCEPT