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.
Related Course: Learn Ruby On Rails For Web Development
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.
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 | apt-get update |
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 | curl -sL https://deb.nodesource.com/setup_8.x | sudo -E bash - |
Node.js is then installed.
Install some gems:
These are required:
1 | gem install puma -v '3.12.0' --source 'https://rubygems.org/' |
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 | rails new blog |
A Ruby on Rails Web Server will open. (in this case at port 3000).
1 | [email protected]:~/blog# rails server |
Open your server address on port 3000 (http://yourserver:3000). You should see “Yay! You’re on 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.