DEV Community

femolacaster
femolacaster

Posted on • Edited on

Journey on Rails: ROR 6’s File Structure

Do you know David Heinemeier Hansson, the creator of ruby on rails is a race driver😲? Interestingly, David has participated in 98 races winning 10 out of 98.

It is apparent David is familiar with voyaging and journeying on a fast lane. No wonder the web framework suits the Rapid Application Development (RAD) development model perfectly.

Maybe, that’s where the concept of the name ruby on rails came from. Realizing a gem while journeying🤔? Who knows🤔? Enough of the assumptions. One fact to note is that his 41st birthday is a month exactly from today and it’s a great opportunity to appreciate Dave for the great framework he created. Moreso, making it open source.

However, it could be a scary journey while traversing around rails for the first time. For some, they begin with a false start running off rails and never coming back to the framework seeing the number of files and directories being created on running the first project setup command:

                  rails new [new-project]
Enter fullscreen mode Exit fullscreen mode

Running a dir*.*/s on windows for the newly created project shows that it has 8,276 directories and 16,729 files. In order not to scare you, rails also ensure this detail is encapsulated by giving you a nice directory structure such as this:

railstruc

It would be nice to breakdown the structure of these files meaningfully. Wouldn’t it? Join me in this journey 🚶 on rails 🚆 as I take you through the file structure so you won’t have a false start.

DON’T MESS WITH THESE FILES

Before we begin this journey, I would be giving out this basic rule. It is best to keep by this unless your journey on rails might be sloppy. It is best not to manipulate with certain files on rails if you don’t have an idea of what they do especially in production. It would even be more disastrous deleting these files. Top-of-the-list is all files in app/config directory. Just as the name goes, the config directory has all the basic configurations for your project including but not limited to environment setup, active records configurations, mailbox configurations, database setup, routing and other configurations.
Some config files you would be using frequently on your journey are config/database.yml for database setup, config/routes.rb for routing, config/application.rb for application-specific configurations, and master.key for storing your application’s secret key. The config/environments allow you to setup rails for different environments such as development, staging, and production. You should also not mess with the bin, lib, and the javascript helpers such as node_modules and the yarn.lock file as they manage binstubs, libraries, webpack and dependencies respectively. And I really don’t see why you would want to modify these files without proper knowledge anyway.

Rails Extensions

Now that we know the rules, we can continue our journey on rails. Seeing a bunch of files and directories may be confusing and one good way to identify them is by the file extension type. I would be explaining some usual file extensions you would see in a rails 6 application:

.coffee

These are coffee scripts majorly based on javascript webpacker’s implementation.

.erb

This is a templating extension where you can write your HTML, js, etc. and wrap your ruby code around it. You would need this mostly in your views.

.git

These are basically files that help manage your source code repositories and versions.

Gemfile

This is where gems and their versions are stored. It has a corresponding lock file that ties the dependencies to the project. Gems are simply plugins, packages, or libraries that would help you code better and lesser. Are they not gems after all?

.js

These are javascript files. You can find javascript files mostly in app/javascript. This would be useful for webpack and websocket configurations.

.json and .lock

package.json is the common culprit inheriting the .json extension. And it basically manages your dependency along with yarn.lock.

.key

Stores your secret key such as master.key. Files with this extension are very sensitive.

.md

This is a text file that can be formatted to suit. The most popular file with this extension is README.md where you can document steps in running your project.

.rb

For me, this is the most important file extension as it contains pure ruby code. This is the ruby file extension.

.rdoc

This is a ruby doc file suitable for comment management and other documentation. Rubydoc is a cool tool similar to javadoc and phpdoc.

.yml

This is used for data-serialization and database.yml uses this approach to store database information.

MVC on Rails

The beauty of ruby on rails is its graceful representation of the MVC structure. MVC helps to separate your code structure aiding for faster development and reusability. In actual fact, rails extend its separation even down to the server level where the app server runs your rails app and puts your code in memory and sends a response to the webserver which then handles the request and returns back to the app server on processing if necessary, else the webserver is self-sufficient. Common web servers for rails are nginx and apache. However, for the app server, we have puma and unicorn while passenger can act as both. The various directories and files that help to bring about a beautiful MVC structure in rails are:

Public

As with a proper MVC secure structure, this folder is specifically set aside for files and folders that would be available to the public.

App/helpers

The helpers directory is where series of re-usable functions that would aid your development is contained.

App/models

This consists of your data representation. The various class building blocks, getter, and setter methods of individual data. Here, you might find database queries used to create various data dimensions.

App/views/layouts

This contains your user interface files to provide functionalities to the end-user.

App/controllers

This holds your business logic and does manipulations based on your application rules.

The journey to getting familiar with rails might be rough. But I can assure you it’s worth it.

Here are some reasons why:

Security

Rails is pretty much a secure framework. I mean a lot of work has been done in making rails as secure as possible. You can read this article here:

https://guides.rubyonrails.org/security.html

Write less code, get much more

With rails, you would be writing just little code because of its excellent file structure and plugins. You would probably be doing more architectural thinking. But I can assure you, a little code written with rails can do much more than expected.

Rails also suggest to you to do things right

With the “convention over configuration” vibe, easier automated testing, version control, off-the-shelf RESTful architecture, seamless separation of development environments, and for the fact ruby can be said to be “strongly typed”, you can hardly write bad code unless you want to anyway.

Some of your favorite apps built with ROR are: Ask.fm, Airbnb, Bloomberg, Dribble, Fiverr, Genius, Goodreads, Scribd, Slideshare, Shopify, and Soundcloud.

And even our darling dev.to!!!!!

Have a great journey on rails ahead!

Top comments (2)

Collapse
 
jdickey profile image
Jeff Dickey

Is anyone still in a use relationship with CoffeeScript any more?

Collapse
 
femolacaster profile image
femolacaster

Well, CoffeeScript has a sweet syntatical sugar whose advantage could be milked for a good breakfast of code. I have not been in a use relationship with CoffeeScript though it keeps calling me. Especially CoffeeScript 2. I guess not all love can be mutual😁. But my love, Ruby on rails hasn't been so jealous and allows for use of CoffeeScript. Anything valuable about CoffeeScript you would like to share when you were in a use relationship with it? Maybe I could show some love to it😁.