Ruby on Rails: Creating an Application on Ubuntu 22.04

Rails is a Ruby-based framework for web applications that provides development standards like routing and asset management. In this tutorial, we build a Rails app to share shark information.

Rails is a web application framework written in Ruby that promotes specific development patterns. It provides conventions for routing, state data, asset management, and more, delivering the core functionality most web applications need. In this tutorial, we will create a Rails application that enables users to publish information about sharks and their behavior.

Prerequisites

For this tutorial, you need a local computer or a development server running Ubuntu 22.04. You should have a non-root user with administrative rights and a firewall configured with ufw. Additionally, you will need Node.js, npm, Ruby, rbenv, and Rails on your local computer or development server.

sudo apt update
sudo apt install sqlite3 libsqlite3-dev

Step 1: Installing SQLite3

Before creating the Rails application, we need a database to store user data. Rails is configured by default to use SQLite, which is often a good choice for development.

sudo apt update
sudo apt install sqlite3 libsqlite3-dev

Step 2: Creating a New Ruby on Rails Project

With the database installed, you can create a new Rails project. Run the following command in the terminal to create a new project named “sharkapp.”

Step 3: Scaffolding the Application

To create the Shark application, we use the command `rails generate scaffold Shark name:string facts:text`.

rails generate scaffold Shark name:string facts:text

Step 4: Setting Up the Application’s Homepage and Testing Functionality

Open the `config/routes.rb` file and define the root route for your application.

Rails.application.routes.draw do
resources :sharks

root ‘sharks#index’
end

Step 5: Adding Validations – Ruby on Rails

Open the `app/models/shark.rb` file and add validations for the `name` and `facts` fields.

class Shark < ApplicationRecord
validates :name, presence: true, uniqueness: true
validates :facts, presence: true
end

Step 6: Adding Authentication

Open the `app/controllers/application_controller.rb` file and use the `http_basic_authenticate_with` method to authenticate users.

class ApplicationController < ActionController::Base
http_basic_authenticate_with name: ‘sammy’, password: ‘shark’, except: [:index, :show]
end

Ruby on Rails – Conclusion

You have now created a functional Rails application that supports data validation and basic authentication. This project can serve as a starting point for further development, and you can learn more about extending your application and integrating other tools by consulting the Rails documentation. Creating a Ruby on Rails Application on Ubuntu 22.04

Create a Free Account

Register now and get access to our Cloud Services.

Posts you might be interested in: