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.
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.
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.
RVM makes sure you have the right version of Ruby and the associated gems. That will install Ruby.
Bundler prevents dependency hell. It tracks and installs the gems you need.
gem install bundler
It is a very useful tool. It can install all the gems for a project with one single command.
Install the web application framework Rails. You can install Rails iwth the gem package manager. The Rails version can be specified.
gem install rails -v 5.2.0
Gem will take care of the Rails installation, but more packages are required.
curl -sL https://deb.nodesource.com/setup_8.x | sudo -E bash -
Node.js is then installed.
These are required:
gem install puma -v '3.12.0' --source 'https://rubygems.org/'
Now that all the dependencies are installed, you can create a Rails app.
Create a new rails project with these commands:
rails new blog
A Ruby on Rails Web Server will open. (in this case at port 3000).
[email protected]:~/blog# rails server
Open your server address on port 3000 (http://yourserver:3000). You should see “Yay! You’re on Rails!”.
A controller has all the business logic for a url request.
Create a new controller with this command:
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
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.
Then delete contents and replace it with this line:
Save and exit. Start the server,
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:
Save and exit
Open the url again (http://server:3000) and you should see the message.
The url /welcome/index should also work.