How to create a Ruby on Rails Application
Learn the basics of creating a simple Ruby on Rails application with step-by-step instructions for making a simple "Hello world" application. This tutorial is a beginner's guide for Ruby on Rails, and is designed for users with no prior Ruby on Rails experience.
Requirements
- 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.
Thanks to free starting credit, you can test the IONOS cloud server for 1 month free of charge (or until the credit is used up) and experience the perfect combination of performance and security!
Install a JavaScript Runtime
You will need to install a JavaScript runtime. Begin by installing the ExecJS gem, which allows you to run JavaScript code with Ruby:
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:
Download the Node.js PPA:
Run the nodesource_setup.sh command to add the PPA to your server's package cache:
This script will update the server automatically. There is no need to run apt-get update a second time.
Install Node.js:
This will automatically install npm as well.
Finally, install the build-essential package for npm:
CentOS 7
Add the EPEL repository:
Then install Node.JS:
Finally, install the Node Package Manager (npm):
- Free Wildcard SSL for safer data transfers
- Free private registration for more privacy
- Free 2 GB email account
The MVC Design
Ruby on Rails uses MVC-based design. MVC stands for Model/View/Controller, and describes the three basic components of a Ruby on Rails app.
Model
The model refers to the database. All of the application's data is stored in the model. Rails uses SQLite by default, but it also supports MySQL and PostgreSQL.
Controllers
The controllers are a set of files which specify which actions to take. The controllers store and retrieve data from the model (database) as needed, and uses this data (as defined by actions) to create the view.
Controllers are directed by routes. These are the rules that define which controllers 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 controllers - the product of all of your hard work on the back-end.
Create and Test the Application 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:
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:
Start the web server with the command:
It should say:
Test it by going to http://your-ip:3000 you should see the default rails welcome page.
When you are done testing the Rails installation, hit Ctrl+C to stop the application and return to the command line.
Create the Application
Create the Controller and Action
We will begin by generating a controller called mainpage:
This will create a number of files:
The first one listed in the command output (app/controllers/mainpage_controller.rb) is the one we will use. Open this file for editing:
We will add an action named hello. Add the following content to this file:
Save and exit the file.
Create the View
Next, we will create a matching view, also named hello:
Add the HTML message to this file:
Save and exit the file.
Create the Route
The route is a rule that will tell Rails which controller to use for this request. Edit the routes.rb file:
Add the line root to: 'mainpage#hello' below the first line, so that the file reads as follows:
This line tells Rails that the root directory / will serve up our controller's action.
Test the Application
Start the Rails server again with the command:
Then test the application by going to http://your-ip:3000. You will see the "Hello, World!" message.