Ruby on Rails is a web application framework. Rails can help you make web apps with Ruby. Some popular apps made with Rails are Basecamp, Github and Shopify.

It’s not a microframework but a metropolis: It can send emails (HTML and plaintext), supports sockets and has other features.

Rails uses the model-view-controller architecture. It divides the app into: business logic (controllers), database (model) and presentation (view).

Learn how to make the “hello world” app in Ruby on Rails.

Rails Tutorial

Setup

Installing ruby and rails is the hardest part. This tutorial has been tested on Ubuntu Linux 18.x

You can install using Ubuntu 18.x in a virtual machine.

I used Vultr to launch a new virtual machine (VM), which makes your web app immediately accesible online.

virtual machine vm vultr

Install rvm

Ruby Version Manager (RVM) is a tool that manages ruby environments and gems. Gem is the package manager for Ruby. A gem is a package.

1
2
3
4
5
6
7
8
apt-get update
sudo apt-get install libgdbm-dev libncurses5-dev automake libtool bison libffi-dev
gpg --keyserver hkp://keys.gnupg.net --recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3 7D2BAF1CF37B13E2069D6956105BD0E739499BDB
curl -sSL https://get.rvm.io | bash -s stable
source /etc/profile.d/rvm.sh
rvm install 2.5.1
rvm use 2.5.1 --default
ruby -v

RVM makes sure you have the right version of Ruby and the associated gems. That will install Ruby.

Install bundler

Bundler prevents dependency hell. It tracks and installs the gems you need.

1
gem install bundler

It is a very useful tool. It can install all the gems for a project with one single command.

Install rails

Install the web application framework Rails. You can install Rails iwth the gem package manager. The Rails version can be specified.

1
gem install rails -v 5.2.0

Gem will take care of the Rails installation, but more packages are required.

Install nodejs

Node.js is required. Node.js is a JavaScript run-time environment that executes JavaScript code server-side.

1
2
curl -sL https://deb.nodesource.com/setup_8.x | sudo -E bash -
sudo apt-get install -y nodejs

Node.js is then installed.

Install some gems:

These are required:

1
2
3
gem install puma -v '3.12.0' --source 'https://rubygems.org/'
gem install 'uglifier'

Rails new project

Create a Rails App

Now that all the dependencies are installed, you can create a Rails app.
Create a new rails project with these commands:

1
2
3
rails new blog
cd blog
rails server

A Ruby on Rails Web Server will open. (in this case at port 3000).

1
2
3
4
5
6
7
8
9
10
[email protected]:~/blog# rails server
=> Booting Puma
=> Rails 5.2.1 application starting in development
=> Run `rails server -h` for more startup options
Puma starting in single mode...
* Version 3.12.0 (ruby 2.5.1-p57), codename: Llamas in Pajamas
* Min threads: 5, max threads: 5
* Environment: development
* Listening on tcp://0.0.0.0:3000
Use Ctrl-C to stop

Open your server address on port 3000 (http://yourserver:3000). You should see “Yay! You’re on Rails!”.
rails

Rails hello world

Create controller

A controller has all the business logic for a url request.
Create a new controller with this command:

1
rails generate controller Welcome index

This will create a controller at app/controllers/welcome_controller.rb
and a view at app/views/welcome/index.html.erb

Create view

A view is what the web browser (Chrome, Firefox) will show to the user. In this case we want it to show “hello world”. Open the view file.

1
nano app/views/welcome/index.html.erb

Then delete contents and replace it with this line:

1
<h1>Hello World</h1>

Save and exit. Start the server,

1
rails server

URL route

The URL route is still missing. An URL Route links the web requests (/welcome/index) to the app. Open the file config/routes.rb in nano.

Add the line:

1
root 'welcome#index'

Save and exit

Open the url again (http://server:3000) and you should see the message.
The url /welcome/index should also work.