Code for Thought

The Magic of Rails

This week we were introduced to rails. At a glance rails just looks like a ton of files that mysteriously appear when a few lines of code are typed into the terminal. I have spent the last week and a half trying to master sinatra, a small and flexible web framework much like rails but with no fancy scaffolding (a term I go into a bit below) and no guiding principles. After having to create many of the files manually in sinatra, seeing a rails app created for the first time left me with a reaction of confusion and awe.

Here I am going to go over some basic rails terminology and principles.

First, What Exactly is Rails?

Rails is an application development framework built using the Ruby language which aims to make the developer’s experience seamless by already including the structure that is needed to create an app with a simple rails new command.

The Rails Guides brag that rails “allows you to write less code while accomplishing more than many other languages and frameworks.” Without a framework like rails, a programmer would have a huge job to implement all the required infrastructure to get an app up and running before they could begin to start creating their app. Rails creates this infrastructure for the developer.

One thing that makes rails popular is its guiding principles:

Don’t Repeat Yourself: Known by the acronym DRY, the Rail’s Guide’s state, “Every piece of knowledge must have a single, unambiguous, authoritative representation within a system.” This principle is to help the developer write code that has less bugs because when code is duplicated, an application becomes more complex, making it more difficult to maintain.

Convention Over Configuration: “Rails has opinions about the best way to do many things in a web application, and defaults to this set of conventions, rather than require that you specify every minutiae through endless configuration files.” This principle means you’ll be productive. You won’t spend time setting up configuration files. You’ll spend less time thinking about where things go and what names to assign. And, because other developers have learned the same conventions, it is easier to collaborate.

Some Basic Terminology

Model/View/Controller pattern - Rails is built around a model-view-controller pattern. This pattern helps in separating the data (model), the logic (controller), and the display (view).

Models - The model represents information that you are using in your application.

Controllers - The controller, as the name suggests, controls the models and the view. It receives user commands and communicates with the models to process the commands. It then tells the view how to display to the user.

Views - The view is how things are visually displayed to the user. It determines how the user interacts with the application and data is presented to the user. The views are written in html or ERB, a ruby embedded language.

CRUD - CRUD is an acronym for the basic operations performed when an app uses a database for storing data. The acronym stands for —

-Create new records in the database

-Read or show the records in the database

-Update existing records

-Destroy or delete records

Scaffolding - Scaffolding is like a build in CRUD wizard. Because these CRUD actions are done so often rails provides commands to make creating them easier.