Tutorials  /  Linux Basics

Ruby on Rails: Creating an Application on Ubuntu 22.04

Ccentron Redaktion · November 2024 ·4 min read ·Linux Basics, Tutorial

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."

rails new 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

VM

Matching infrastructure at centron

No hardware needed to follow along: ccloud³ VMs with full root access start at €3.12 per month, billed by the hour and ready in seconds. Rent a cloud server →

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

Jetzt 200 € Guthaben sichern

Testen Sie Ihr Setup auf ccloud³

Registrieren Sie sich in der ccloud³ und erhalten Sie 200 € Startguthaben für Ihr Projekt – z. B. für eine PostgreSQL-VM mit automatischen Backups.

centron Redaktion Technische Redaktion

Das Redaktionsteam von centron schreibt Anleitungen aus dem Betriebsalltag: getestet auf unserer eigenen Plattform, betrieben im Rechenzentrum in Hallstadt bei Bamberg.

Kategorie Linux Basics
Teilen
Noch offene Fragen?

Our team will help you with your specific setup - in German or English, by people who run the platform themselves.

War dieses Tutorial hilfreich?

Your answer is stored anonymously and helps us improve our tutorials.

Kommentare

No comments yet - be the first to ask a question about this tutorial.

Sign in to comment

Comments are open to centron customers. Sign in to your account to ask a question about this tutorial.

Weiterlesen

Das könnte Sie auch interessieren

Jetzt kostenlos anfangen

Melden Sie sich an und erhalten Sie in den ersten 60 Tagen ein Guthaben von 200 € bei centron.

Dieses Werbeangebot gilt nur für neue Konten. Angebot ausschließlich für Gewerbetreibende.

Jetzt loslegen Sales kontaktieren