Support Online
Skip to main content

Developing a Ruby on Rails Application from Scratch

🧠 Technical Summary

In this guide, you will develop a fully functional web application using Ruby on Rails in an Ubuntu 18.04-based development environment. The goal is to add database-based CRUD operations, data validation, and user authentication using Rails' Model–View–Controller (MVC) architecture.

Application example: sharkapp Database: SQLite3 Target: CRUD operations, verification and authentication


💡 What You Will Learn in This Guide

  • Understanding the MVC structure of Rails
  • Quick project creation with rails new and scaffold commands
  • Setting up a SQLite database and executing migration operations
  • Adding model validations
  • Implement basic HTTP authentication

⚙️ 1. Setting Up the Database Infrastructure (SQLite3)

Rails uses SQLite by default in the development environment. You can install with the following commands:

sudo apt update
sudo apt install sqlite3 libsqlite3-dev
sqlite3 --version

These commands install SQLite and the necessary development files.


🧩 2. Creating a New Rails Project

Start a new application:

rails new sharkapp
cd sharkapp

This command creates the basic directory structure, Gemfile and default MVC components.

Start the Rails server:

rails server

In the browser, go to http://localhost:3000. The Rails welcome screen should appear.

If you are working on a remote development server:

sudo ufw allow 3000
rails server --binding=tr1-sunucu-ip

Then visit http://tr1-sunucu-ip:3000.


🧱 3. Creating Model and CRUD Resources

Create a model named Shark and CRUD resources:

rails generate scaffold Shark name:string facts:text

This command automatically generates model, controller, view and migration files.

Update the database:

rails db:migrate

Migration creates the sharks table and makes it ready for use.


🌐 4. Adjusting the App's Main View

Configure the home page (root) to show the Shark list:

# ~/sharkapp/config/routes.rb
Rails.application.routes.draw do
resources :sharks
root 'sharks#index'
end

You will now see the Shark listing at http://localhost:3000.


🧠 5. Adding Data Validations

Add validation rules to ensure consistency in data entries:

# ~/sharkapp/app/models/shark.rb
class Shark < ApplicationRecord
validates :name, presence: true, uniqueness: true
validates :facts, presence: true
end

These rules prevent empty or duplicate records from being saved to the database.


🔒 6. Basic Authentication

Add HTTP Basic Authentication to be able to modify application context:

# ~/sharkapp/app/controllers/application_controller.rb
class ApplicationController < ActionController::Base
http_basic_authenticate_with name: 'sammy', password: 'shark', except: [:index, :show]
end

All actions require input except actions index and show.

Click on the “New Shark” or “Edit” link in the browser and the authentication window will open.


🧪 7. Testing the App

Start the Rails server:

rails s

Go to http://localhost:3000. Test CRUD operations by adding and editing a new “Shark” record. If you leave incorrect or empty space, model validation will come into play.


❓ Frequently Asked Questions (FAQ)

1. What does MVC architecture do?

It keeps the code organized by separating the Model (data and business logic), View (interface) and Controller (request management) layers.

2. Is it a good idea to use scaffold?

Provides quick start in small projects. In large projects, manual control is preferred.

3. What is Active Record validation?

It is the structure that allows you to control data entry in the model layer of Rails.

4. Is Basic Auth secure?

Suitable for development environment. It is recommended to use Devise or bcrypt in a production environment.

5. What do Rails routes do?

It directs incoming HTTP requests to the correct method in the correct controller.


🎯 Result

You've now developed an MVC application from scratch using Ruby on Rails! You have implemented basic security layers such as model validation and authentication in your project. 💡 You can immediately test and scale your app on a development server on GenixNode.