Ruby on Rails

📄 Table of Contents


  • Ruby on rails is an open source web framework written in the Ruby programming language, and all the applications in Rails are written in Ruby.

Ruby is to Rails as PHP is to Symfony and Zend, or as Python is to Django. The appeal of Ruby to developers lies in the elegance and terseness of the language.

  • The Rails framework helps developers to build websites and applications, because it abstracts and simplifies common repetitive tasks. It is also one of the most popular web application frameworks amongst developers.


  • One of the many reasons why ruby on rails web development companies choose RoR as their technology is because it has the ability for rapid prototyping.
  • Another key principles of Ruby on Rails development (henceforth ‘Rails’) is convention over configuration. This means that the programmer does not have to spend a lot of time configuring files in order to get setup, Rails comes with a set of conventions which help speed up development.
  • Next characteristic of Rails is the emphasis on RESTful application design. REST (Representational State Transfer) is a style of software architecture based around the client-server relationship. It encourages a logical structure within applications, which means they can easily be exposed as an API (Application Programming Interface).

From project management point of view, the Ruby on Rails community advocate Agile web development — an iterative development method, that encourages collaborative and flexible approach, which is particularly well-suited for web application development with fast-changing requirements.

  • Rails is good for rapid application development (RAD), as the framework makes it easy to accommodate changes.
  • Ruby on Rails uses the Model-View-Controller (MVC) architectural pattern. MVC is a pattern for the architecture of a software application. It separates an application into the following components:

Model is a database + for handling data and business logic

View is your visual aspects : the html + css, etc. + for handling graphical user interface objects and presentation

Controller helps the model and the view talk to each other.

This separation results in user requests being processed as follows:

  1. The browser sends a request for a page to the controller on the server
  2. The controller retrieves the data it needs from the model in order to respond to the request
  3. The controller gives the retrieved data to the view
  4. The view is rendered and sent back to the client for the browser to display


  • Not all website hosts can support Rails. However, this is by no means a deal-breaker, and of course Rails-friendly hosts do exist, for example, Heroku and EngineYard.

There is also the option of running your application under JRuby, so you have the same performance characteristics as Java.

ROR Commands

So, to recap, keep these 5 helpful commands handy when your building your next MVC app:

  1. $gem install rails → installs rails

2. $rails new <project-name> → creates a new project

3. $rails generate <controller-name> <view-name>

creates a controller (make sure it’s plural) + a corresponding .html.erb view file as a bonus + layout .

For example if we have a controller like this:

class SamplesController < ApplicationController
def show
render ‘show’ #this can be omitted

show is a method of SamplesController class. If no action is defined then it simply renders the view along with layout. We should have a a view file inside app/views/samples called show.html.erb. Also, if we don’t have any layout, simply put layout false.

4. $rails generate model name [field[:type] field[:type]

creates our model and also specify what we’d like to store in our schema. With the $rails generate model command, we can name our model and also specify what we’d like to store in our schema.

But why go through the trouble of manually creating your own models and controllers when you can generate all of this and more with $rails generate scaffold?

5. $rails generate scaffold

With Rails generate scaffold, we can generate our model, a database migration for the model, and a controller with actions and views! All done with one command.

Scaffolding is like a shortcut. Scaffolding automatically generates the models, views and controllers you need for a table. A quick google search tells us:

Scaffolding in Ruby on Rails refers to the auto generation of a simple set of a model, views and controller usually for a single table. Would create a full CRUD (create, read, update, delete) web interface for the Users table”

ROR syntax

def index
redirect_to(controller => “demo”, action => “index”)
<%= 5 + 5 %>
<% var = “rubyOnRails” %>
<%= “hello” #{ var } %>
@instance_var =[1, 2, 3, 4, 5]// how to link to other pages
< a href = “/demo/hello” > Hello page </a >
<%= link_to(“page to link to”, {action => “hello”})


A view need not actually contain any Ruby code at all — it may be the case that one of your views is a simple HTML file; however, it’s more likely that your views will contain a combination of HTML and Ruby code, making the page more dynamic. The Ruby code is embedded in HTML using embedded Ruby syntax.

Styling in ROR

There are 3 basic categories of styling that we can use to manipulate the html in our apps and make it more presentable. These categories include inline, internal and external styling.

It is advised that we never use this first 2 forms of styling when coding.

<h1 style=”color:blue”>Hello Rubyers!</h1><style type=”text/css”> 
h1{color: blue;}

[3]. External Styling

The third category of styling would fall under external styling which is when a separate css file is created and all of the styling syntax is placed in that separate file.

When creating a rails application we are welcomed by a not so blank canvas that will be used for code. In the sea of different files created we can follow the path app/assets/stylesheets and create a new file style.css under the stylesheets folder. This file can be called anything but make sure you end it with the .css extension.

Now that we have a .css file we can move to app/views/layouts and open the application.html.erb file and enter a stylesheet link tag that will connect the applications html file with the stylesheet. The string should be the name of your stylesheet.

<%= stylesheet_link_tag 'style.css' %>

With this done we are ready to begin to style and make simple plain text pages into something completely different. Remember when adding your styling logic always add a semicolon<declaration separator> at the end of each declaration.

What is @/@@?

There are 4 types of variables in Ruby: local , global, instance, and class. The @ symbol before a variable tells Ruby that we are working with an instance variable, and @@ before a variable tells us we are working with a class variable. We use @ before a variable in instance methods within a class to tell Ruby to access that attribute (instance variable) of the instance. Using @instance_var makes instance_var accessible to all instance methods. Similarly, we use @@ before a variable in class methods to tell Ruby to access that attribute (class variable) of the class. Using @@class_var makes class_var accessible to the entire class. When we use @instance_var or @@class_var, we are accessing that instance or class variable directly.

Here is an example:

What is self?

You may have noticed the word self in the definition our class methods. Every object in Ruby has and is aware of its self. The keyword self in Ruby enables you to access to the current object — the object that is receiving the current message.

Setup Redis on Mac

type below:

brew update
brew install redis

To have launchd start redis now and restart at login:

brew services start redis

to stop it, just run:

brew services stop redis

Test if Redis server is running.

redis-cli ping

If it replies “PONG”, then it’s good to go!

Or, if you don’t want/need a background service you can just run:

redis-server /usr/local/etc/redis.conf

Location of Redis configuration file.


Uninstall Redis and its files.

brew uninstall redis
rm ~/Library/LaunchAgents/homebrew.mxcl.redis.plist

Install Elasticsearch on macOS with Homebrew

Elastic publishes Homebrew formulae so you can install Elasticsearch with the Homebrew package manager.

To install with Homebrew, you first need to tap the Elastic Homebrew repository:

brew tap elastic/tap

Once you’ve tapped the Elastic Homebrew repo, you can use brew install to install the default distribution of Elasticsearch:

brew install elastic/tap/elasticsearch-full

This installs the most recently released default distribution of Elasticsearch. To install the OSS distribution, specify elastic/tap/elasticsearch-oss.

Install/Check Mongodb on macOS

To check if mongoose module is installed simply find the version by

npm list mongoose

To check globally

npm list -g  mongoose

If you are using MongoLab, to connect to database type: mongo

If you are running Mongo locally, start the Mongo daemon:

In the terminal type: mongod
If that doesnt run try: sudo mongod
Once mongoDB is running open another terminal window and type: mongo
In the same window type: show dbs
To work with a database in the list named myDB, for example type : use myDB

Initially released in 2010, Elasticsearch (sometimes dubbed ES) is a modern search and analytics engine which is based on Apache Lucene. Completely open source and built with Java, Elasticsearch is a NoSQL database. That means it stores data in an unstructured way and that you cannot use SQL to query it

Ruby on Rails — HTML.ERB

HTML.ERB is HTML mixed with Ruby, using HTML tags. All of Ruby is available for programming along with HTML.

Following is the syntax of using Ruby with HTML −

<% %>   # executes the Ruby code
<%= %> # executes the Ruby code and displays the result


<% @products.each do |p| %>
<li> <%= %> </li>
<% end %>

The output of anything in <%= %> tags is directly copied to the HTML output stream. To secure against HTML injection, use the h() function to html_escape the output

For example

<%=h @user_entered_notes %>


The first thing you want to do is open up your “app/views/layouts/application.html.erb” file and merge in the markup from Tim’s “homepage.html” file that is going to be on all our pages:

render & render partial:

  • render 'some_view' is a shorthand for render partial: 'some_view'.
  • render file: 'view' will look for a file view.html.erb and NOT _view.html.erb (.erb or any other renderer you use)
  • render will not accept additional local variables for the partial, you need to use render partial: as following for that:

render partial: 'some/path/to/my/partial', locals: { custom_var: 'Hello' }

yield & content_for

  • yield is typically used in layouts. It tells Rails to put the content for this block at that place in the layout.
  • When you do yield :something associated with content_for :something, you can pass a block of code (view) to display where the yield :something is placed (see example below).

A small example about yield:

In your layout:

<%= yield :html_head %>
<div id="sidebar">
<%= yield :sidebar %>

In one of your view:

<% content_for :sidebar do %>
This content will show up in the sidebar section
<% end %>
<% content_for :html_head do %>
<script type="text/javascript">
console.log("Hello World!");
<% end %>

This will produce the following HTML:

<script type="text/javascript">
console.log("Hello World!");

<div id="sidebar">
This content will show up in the sidebar section

How do I auto format Ruby or .erb files in VS Code?

You can set format associations in VSCode, so .erb files would be treated like .html.

Go to File->Preferences->Settings->Click … in top right corner->Open settings.json

Then add this piece of code to your settings.json

"files.associations": {
"*.html.erb": "html"

This is how I solved this problem. It will remove some of the code highlights but will autoformat HTML templates like an HTML document.

Use CSS to automatically add ‘required field’ asterisk to form inputs

<div class="required">
<input type="text">
.required:after { content:" *"; }




Experience with Front-end Technologies and MERN / MEAN Stack. Working on all Major UI Frameworks like React, Angular.

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Mordor Labs 😈 — Part 3: Executing ATT&CK APT29 Evaluations Emulation Plan 📕 -Day2

API Testing with Cypress — Part 1

Cheatsheet for Django Models

#control flow statement in java:-

Improve data quality by using the pandas library and Python

Spot-On Fleet Admin: 13 Basic AWS EC2 Mgmt Console Filters in Action

Python sample quiz

Project-3) Scrape/Download Tweets from Twitter with Python using Snscrape

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Anil Kumar

Anil Kumar

Experience with Front-end Technologies and MERN / MEAN Stack. Working on all Major UI Frameworks like React, Angular.

More from Medium

Ruby on Rails in a Nutshell

Rails Project Reflection — A Ruby on Rails Recipe Manager

Implement Stack in Ruby to Solve Problem

Three Design Patterns (Factory, Abstract Factory, Builder) and Their Usage with Ruby