Learn the basics of creating a simple Ruby on Rails ap­pli­ca­tion with step-by-step in­struc­tions for making a simple "Hello world" ap­pli­ca­tion. This tutorial is a be­gin­ner's guide for Ruby on Rails, and is designed for users with no prior Ruby on Rails ex­pe­ri­ence.

Re­quire­ments

  • A Cloud Server running Linux (Ubuntu 16.04 or CentOS 7)
  • Ruby on Rails installed and running.
  • If you have a firewall, you will need to allow access to port 3000.
Free Cloud Server Trial
En­ter­prise-grade virtual private servers
  • KVM based dev servers for de­vel­op­ers
  • Scalable to en­ter­prise cloud level
  • Pay-as-you-go, per-minute billing

Install a JavaScript Runtime

You will need to install a JavaScript runtime. Begin by in­stalling the ExecJS gem, which allows you to run JavaScript code with Ruby:

gem install execjs

If you have a preferred runtime, feel free to install it next. Otherwise, we will install Node.JS for this project.

Ubuntu 16.04

Update your server's packages and install curl with the following commands:

sudo apt-get update
sudo apt-get install curl

Download the Node.js PPA:

curl -sL https://deb.nodesource.com/setup_6.x -o nodesource_setup.sh

Run the nodes­ource_setup.sh command to add the PPA to your server's package cache:

sudo bash nodesource_setup.sh
Note

This script will update the server au­to­mat­i­cal­ly. There is no need to run apt-get update a second time.

Install Node.js:

sudo apt-get install nodejs

This will au­to­mat­i­cal­ly install npm as well.

Finally, install the build-essential package for npm:

sudo apt-get install build-essential

CentOS 7

Add the EPEL repos­i­to­ry:

sudo yum install epel-release

Then install Node.JS:

sudo yum install nodejs

Finally, install the Node Package Manager (npm):

sudo yum install npm
Domain Name Reg­is­tra­tion
Build your brand on a great domain
  • Free Wildcard SSL for safer data transfers 
  • Free private reg­is­tra­tion for more privacy
  • Free Domain Connect for easy DNS setup
Domain Checker

The MVC Design

Ruby on Rails uses MVC-based design. MVC stands for Model/View/Con­troller, and describes the three basic com­po­nents of a Ruby on Rails app.

Model

The model refers to the database. All of the ap­pli­ca­tion's data is stored in the model. Rails uses SQLite by default, but it also supports MySQL and Post­greSQL.

Con­trollers

The con­trollers are a set of files which specify which actions to take. The con­trollers store and retrieve data from the model (database) as needed, and uses this data (as defined by actions) to create the view.

Con­trollers are directed by routes. These are the rules that define which con­trollers will perform which actions. Routes are defined in the routes.rb file.

View

The view is the end result, what a visitor sees in a browser. It is the response to the actions that are defined by the con­trollers - the product of all of your hard work on the back-end.

Create and Test the Ap­pli­ca­tion Structure

The rails new command will create a file structure for your app. Go to the directory where you want to store Ruby on Rails projects and use the command:

rails new hello-world
Note

Remember to be logged in as the Ruby user.

At this point you can do a quick test to make sure that Ruby on Rails is set up and running correctly. Go to the new hello-world directory which was created by the previous command:

cd hello-world

Start the web server with the command:

bin/rails s --binding=0.0.0.0

It should say:

[user@localhost blog]$ bin/rails server
=> Booting Puma
=> Rails 5.0.0.1 application starting in development on http://localhost:3000
=> Run `rails server -h` for more startup options
Puma starting in single mode...
* Version 3.6.0 (ruby 2.3.1-p112), codename: Sleepy Sunday Serenity
* Min threads: 5, max threads: 5
* Environment: development
* Listening on tcp://localhost:3000
Use Ctrl-C to stop

Test it by going to http://your-ip:3000 you should see the default rails welcome page.

When you are done testing the Rails in­stal­la­tion, hit Ctrl+C to stop the ap­pli­ca­tion and return to the command line.

Create the Ap­pli­ca­tion

Create the Con­troller and Action

We will begin by gen­er­at­ing a con­troller called mainpage:

rails generate controller mainpage

This will create a number of files:

[user@localhost hello-world]$ rails generate controller mainpage
Running via Spring preloader in process 24461
     create  app/controllers/mainpage_controller.rb
     invoke  erb
     create    app/views/mainpage
     invoke  test_unit
     create    test/controllers/mainpage_controller_test.rb
     invoke  helper
     create    app/helpers/mainpage_helper.rb
     invoke    test_unit
     invoke  assets
     invoke    coffee
     create     app/assets/javascripts/mainpage.coffee
     invoke    scss
    create     app/assets/stylesheets/mainpage.scss

The first one listed in the command output (app/con­trollers/mainpage_con­troller.rb) is the one we will use. Open this file for editing:

nano app/controllers/mainpage_controller.rb

We will add an action named hello. Add the following content to this file:

class MainpageController < ApplicationController
    def hello
    end 
end

Save and exit the file.

Create the View

Next, we will create a matching view, also named hello:

nano app/views/mainpage/hello.html.erb

Add the HTML message to this file:

<h1>Hello, world!</h1>

Save and exit the file.

Create the Route

The route is a rule that will tell Rails which con­troller to use for this request. Edit the routes.rb file:

nano config/routes.rb

Add the line root to: 'mainpage#hello' below the first line, so that the file reads as follows:

Rails.application.routes.draw do
    root to: 'mainpage#hello'
    # For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
end

This line tells Rails that the root directory / will serve up our con­troller's action.

Test the Ap­pli­ca­tion

Start the Rails server again with the command:

bin/rails s --binding=0.0.0.0

Then test the ap­pli­ca­tion by going to http://your-ip:3000. You will see the "Hello, World!" message.

Go to Main Menu