Ruby on Rails (often called Rails) is an open-source, web application development framework written in the Ruby programming language. Rails makes it fun and easy to program web applications: it allows you to write less code but accomplish more than most frameworks.
The Rails framework is opinionated, so it operates on the assumption that there’s a “best” way to make things. In this tutorial, we will introduce you to Rails and show you how to make a web application.
This tutorial at a glance:
In this course, you will learn the fundamentals of Ruby on Rails to build your own application. By the end, you’ll be able to build complex applications.
Rails is a powerful, extremely opinionated, full-stack web application framework build on top of the back-end Ruby programming language. It is used to build powerful web apps quickly due to its innovative functionalities, such as table migrations and scaffolds.
Rails offers a default structure to organize your code and make workflow easier. Programmers who use Rails say that is makes software development far more fun and simple.
Rails development is based on the Model-View-Controller ( MVC) architecture to organize application programming and is guided by the Don’t Repeat Yourself (DRY) principle of software development.
Rails still remains popular in the world of web app development. Rails is very mature framework with tons of active support. Tons of companies are looking for talented Rails developers, since this framework minimizes app production time.
Some of the big companies who use Rails include AirBnB, GitHub, Basecamp, Shopify, Soundcloud, and Goodreads. It is also popular amongst many startups due to speed.
Facts: The average base salary for a Rails developer is $116,437 USD.
Anyone can learn Rails, even if you have limited experience with Ruby code. Rails is full-stack and offers many integrated tools for rapidly creating dynamic applications. Here are some of the salient features that make Rails so powerful:
To get more familiar with Rails before building our own app, let’s look at a summary of the standard file and directory structure of any Rails application.
app/: contains core application code (controllers, models, views, helpers, mailers, channels, jobs, and assets)
bin/: contains binary executable files (scripts) to start, update, deploy, or run your Rails app
config/: contains the configuration files for the application, routes, database, etc.
db/: contains database files, database schema, and database migrations
lib/: contains extended library modules
log/: contains the application log files
public/: contains data that is accessible via browsers (static files, compiled assets, error pages, etc.)
storage/: contains active storage files
test/: contains application unit tests, fixtures, and various other test apparatus
tmp/: contains temporary files (like cache and pid files)
vendor/: contains any third party code
Gemfile: contains the gem requirements for the application, required for Ruby to run the app
Gemfile.lock: contains a list of gems to ensure all copies of the application use the same version
Rakefile: allows the command line to locate and run
rake tasks (a Make-like build utility implemented in Ruby for task automation)
config.ru: contains configuration for the Rack middleware based servers used to start the application
package.json: allows you to specify
npm dependencies for your Rails app
If you don’t have it already, you’ll need to install Rails. First check that your system has the following prerequisites:
gem installcommand provided by RubyGems.
$ gem install rails
Then, verify that everything is installed correctly by running the following:
$ rails --version
It should say something along the lines “Rails 6.0.0”. Now you’re good to create a project.
Rails comes with built-in scripts called generators that automatically create everything you need to start a new project. To create a new application, you invoke the
Think of this as the foundation for your whole project. The
new generator sets up a project in the
my_project directory with an application structure.
To use the
new generator, we run this command:
rails new my_project
Now you should have an application. We now use the Rails script to run the Puma web-server, which will deploy our Rails application. We
cd into the project folder and run the following command:
You can also run the server with additional parameters.
Learn Ruby on Rails without scrubbing through videos or documentation. Educative’s text-based courses are easy to skim and feature live coding environments - making learning quick and efficient.
Let’s get Rail to say “Hello”. To do this, we need to create a view, route, and controller with an action. Routes will map a request to our controller action. A controller action handles the request and prepares the data for our view. The view simply displays our data in the format we want it.
These features are part of Rails’ MVC design pattern.
MVC is a design pattern that separates the program logic into three elements: models, views, and controllers. This separates the application data and the code. Rails places almost the entire model, view, and controller logic on the server-side.
Syntax: Routes are rules written in a Ruby DSL (Domain-Specific Language). Controllers are essentially just Ruby classes. Any public methods are actions. Views are templates in a HTML and Ruby.
First, we create a customer controller and a view. To make a new controller, we run the controller generator script in the project root directory of
rails generate controller Hello index
This creates a controller called
Hello and an action called
Our controller is located here:
app/controllers/hello_controller.rband our view is located here
.erb extension above means that the file is written in Embedded Ruby, which is the default for view templates.
html.erb files, you can use tags to embed Ruby code into HTML.
<% %>: You can insert any Ruby code between these tags, and the result will not be inserted into your HTML. This is used for control flow statements.
<%= %>: Ruby code between this tag gets inserted into the HTML at its position. This is used to retrieve data from models.
index.html.erb file, we add the line:
<h1>Hello World on Rails!</h1>
This is what will be displayed on the application’s root page
/. However, if we run the app now, we will go the old page because the root is currently occupied by a page created when we started the program.
To change this, we make changes to our
config/routes.rb file. Essentially, we have to tell the file where the new home page is located.
Your routing file will connect URLs to our code and create paths from your code.
The general syntax for defining a route is as follows:
For our purposes, the
hello, and our action_name is
index. Here is an example f a GET request.
But, we are defining the root of our Rails application, so our syntax is different.
Browsers make requests to the Rails application using HTTP methods (GET, PUT, POST, PATCH, and DELETE). Each of these HTTP methods requests to perform an operation on your application.
routes.rb will be as follows:
Rails.application.routes.draw do get 'hello/index' root 'hello#index' # For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html end
Now, when we run the application, it will display our desired
“Hello World on Rails!” message.
Rails Model is a Ruby class that for adding database records. Generally, a models should provide four basic functionalities:
These functionalities represent the acronym CRUD.
Rails also uses the Active Record framework for creating and using our models. This is an Object Relational Mapping (ORM) framework to connect objects of an application with tables in a relational database management system (RDBMS).
Active Record means we don’t need to write SQL statements directly, so storage and retrieval of properties is much easier.
Think of this as an interface between your database tables and the Ruby code.
The naming convention for the Active Record is as follows:
Let’s apply this to our app. Suppose you are building a Pet store application. We need to store details for each pet (such as name and age). We need a database and a model.
Let’s create a new
pet_store application to understand CRUD better.
rails new pet_store
To create a new model, the syntax is:
rails generate model ModelName ColumnName:ColumnType ColumnName:ColumnType
Now we run the command to generate a new model inside the project folder we made:
rails generate model Pet name:string age:string
This generates two files:
db/migrate/<date_time>_create_pets.rb: the migration file
app/models/pet.rb: file for your model
Congrats on making it to the end! You should now have the foundations to create your own Ruby on Rails applications. There is still a lot to learn. The next steps are to understand:
To get hands-on practice with these, check out Educative’s course Introduction to Ruby on Rails. You will learn the fundamentals of Ruby on Rails and start by learning how to create a new project, deploy to a Rails server, and the Rails design pattern. After that, you’ll learn how to perform CRUD operations, handle user authentication, and manage sessions.
By the end, you’ll be able to build complex Rails applications and add this skill to your portfolio.
Join a community of 270,000 monthly readers. A free, bi-monthly email with a roundup of Educative's top articles and coding tips.