-
Notifications
You must be signed in to change notification settings - Fork 2
An Explainer on the Folder Structure of a Rails App
This is an explainer guide to help a beginner or a newbie developer, understand the folders present in the repository. It was built from this post.
When you first look at the repository, you might be wondering how to access necessary files in Rails. Like where can I type the code to create the routes in the API or what is the helpers folder. This is a summary guide and links to other posts/guides, to help you better understand this.
The folders have been created using the command rails new -appname
. In these folders, Rails knows where to find things it needs, so you don't have to provide any input. This is just a guide to help you understand where the files you will need are kept.
You can access these locally on your computer after you have cloned the repository to your local system. After the repository has been cloned, you can access the files via your terminal, via your file explorer, or via your code editor. This will be explained in a later post.
Using your code text editor (like Sublime or Atom or VS Code) to access the folder after it has been cloned, you will find a directory structure in your text editor like the image below:
Each of the folders are explained below:
- app − The app is the first folder. It contains the model, controller, views, helpers, concerns, CSS & JS files. It basically organizes your app components.
- app/controllers − When you click on app you will see the controllers directory. Each controller contains the necessary actions.
- app/helpers − The helpers are good for keeping the controller, model and views code clean and focused. Especially when you see yourself doing a lot of logic in your views you can put it in a method, inside your helper instead.
- app/models − The models holds the classes that model and wrap the data stored in our application's database. In most frameworks, this part of the application can grow pretty messy, tedious, verbose, and error-prone. Rails makes it very simple!
- app/view − This is where all your display templates live, they all have an extension of
.erb
. These files get to be converted to HTML before its returned to the user's browser. - app/view/layouts − Normally in a plain html boiler template you will have header, sidebar and footer repeated in all html files, the layout helps keep your view neat by avoid this repetition of html code.
- config − The config directory includes all configuration files like your database.yml (configuration setting for your database), routes.rb (where all your routes are defined), environment.rb (your rails environment structure). You can also tailor the behavior of the three Rails environments for test, development, and deployment with files found in the environments directory.
- db − This includes your schema, migration files, and seed_data.
- test/spec − When you generate a rails app by default it uses minitest and this is where all test files are kept. But when using Rspec Framework all spec files are in a directory spec. This where all test The tests you write and those that Rspec Rail creates by default stays.
Thanks to Susan Esho for helping with this document. I only copied, edited and pasted 😉