Ruby on Rails: A Practical Guide for Modern Web Development

Ruby on Rails changed web development when it launched in 2004, and it’s still going strong today. If you’re looking for a framework that lets you build powerful web applications quickly without sacrificing code quality, Rails deserves your attention. Let’s dive into what makes Rails special and get you started with real examples.

Why Rails Still Matters in 2025

Rails introduced concepts that became industry standards: Convention over Configuration (CoC), Don’t Repeat Yourself (DRY), and RESTful architecture. While newer frameworks have emerged, Rails continues to power major platforms like GitHub, Shopify, Basecamp, and Airbnb.

The framework’s productivity boost comes from its opinionated structure. Rails makes decisions for you about project organization, database interactions, and routing. This means less time configuring and more time building features.

Key advantages:

  • Rapid development with scaffolding and generators
  • Massive ecosystem of gems (libraries)
  • Active Record ORM simplifies database operations
  • Built-in testing framework
  • Strong community and extensive documentation

Setting Up Your Environment

Before we code, let’s get Rails installed. You’ll need Ruby (version 3.0+) and Rails (version 7.0+).

Installation steps:

# Install Ruby (using rbenv or RVM recommended)
# Then install Rails
gem install rails

# Verify installation
rails --version

You’ll also want PostgreSQL or MySQL for production apps, though Rails works with SQLite out of the box for development.

Hello World: Your First Rails App

Let’s create the simplest possible Rails application to understand the basics.

Step 1: Create the application

rails new hello_world
cd hello_world

This generates your entire project structure: controllers, models, views, config files, and more.

Step 2: Generate a controller

rails generate controller Welcome index

This creates a WelcomesController with an index action and a corresponding view.

Step 3: Set the root route

Edit config/routes.rb:

Rails.application.routes.draw do
  root 'welcome#index'
end

Step 4: Create your view

Edit app/views/welcome/index.html.erb:

<h1>Hello World!</h1>
<p>Welcome to Ruby on Rails</p>
<p>The time is: <%= Time.now %></p>
<h1>Hello World!</h1>
<p>Welcome to Ruby on Rails</p>
<p>The time is: <%= Time.now %></p>

Step 5: Start the server

rails server

Visit http://localhost:3000 and you’ll see your Hello World page. Notice the ERB (Embedded Ruby) syntax – <%= %> executes Ruby code and displays the result.

Building a To Do List App: Step by Step

Now let’s build something more practical – a to do list application that demonstrates CRUD operations (Create, Read, Update, Delete).

Step 1: Create the Project

rails new to_do_list_app
cd to_do_list_app

Step 2: Generate the Task Model

Rails scaffolding creates everything you need for a resource:

rails generate scaffold Task title:string description:text completed:boolean

This single command creates:

  • Migration file for the database
  • Task model
  • Tasks controller with all CRUD actions
  • Views for index, show, new, edit
  • Routes

Step 3: Run the Migration

rails db:migrate

This creates the tasks table in your database with the specified columns plus automatic id, created_at, and updated_at fields.

Step 4: Understanding the Model

Open app/models/task.rb:

class Task < ApplicationRecord
  validates :title, presence: true
  
  scope :completed, -> { where(completed: true) }
  scope :pending, -> { where(completed: false) }
end

Add validations to ensure data integrity. Scopes provide convenient query methods.

Step 5: Customize the Controller

The generated controller (app/controllers/tasks_controller.rb) already has all CRUD actions. Let’s look at the create action:

def create
  @task = Task.new(task_params)
  
  if @task.save
    redirect_to @task, notice: 'Task was successfully created.'
  else
    render :new, status: :unprocessable_entity
  end
end

private

def task_params
  params.require(:task).permit(:title, :description, :completed)
end

The task_params method uses strong parameters to prevent mass assignment vulnerabilities.

Step 6: Improve the Views

Edit app/views/tasks/index.html.erb to make it more functional:

<h1>EF To Do List</h1>

<%= link_to "New Task", new_task_path, class: "btn btn-primary" %>

<h2>Pending Tasks</h2>
<% @tasks.pending.each do |task| %>
  <div class="task">
    <h3><%= link_to task.title, task %></h3>
    <p><%= task.description %></p>
    <%= link_to "Complete", task_path(task, task: { completed: true }), method: :patch %>
    <%= link_to "Edit", edit_task_path(task) %>
    <%= link_to "Delete", task, method: :delete, data: { confirm: "Are you sure?" } %>
  </div>
<% end %>

<h2>Completed Tasks</h2>
<% @tasks.completed.each do |task| %>
  <div class="task completed">
    <h3><%= task.title %></h3>
    <p><%= task.description %></p>
  </div>
<% end %>

Step 7: Add Some Style

Rails uses the asset pipeline. Create app/assets/stylesheets/tasks.css:

.task {
  border: 1px solid #ddd;
  padding: 15px;
  margin: 10px 0;
  border-radius: 5px;
}

.task.completed {
  opacity: 0.6;
  text-decoration: line-through;
}

.btn {
  padding: 10px 20px;
  background-color: #007bff;
  color: white;
  text-decoration: none;
  border-radius: 3px;
}

Step 8: Test Your App

rails server

Visit http://localhost:3000/tasks to see your to-do list in action. You can now create, edit, complete, and delete tasks.

Practical Tips for Rails Development

Use the Rails Console

The console is invaluable for testing code and debugging:

rails console
# Now you can interact with your models
Task.create(title: "Learn Rails", completed: false)
Task.all
Task.where(completed: true).count

Leverage Gems

Don’t reinvent the wheel. Popular gems include:

  • devise for authentication
  • pundit or cancancan for authorization
  • kaminari or pagy for pagination
  • sidekiq for background jobs
  • rspec-rails for testing

Follow Rails Conventions

Rails rewards convention. Name your files correctly, use RESTful routes, and organize code in the expected structure. The framework does the heavy lifting when you follow the rules.

Keep Controllers Thin

Business logic belongs in models, not controllers. Controllers should orchestrate, not implement logic.

Write Tests

Rails makes testing easy with built-in support for unit, integration, and system tests:

# test/models/task_test.rb
require "test_helper"

class TaskTest < ActiveSupport::TestCase
  test "should not save task without title" do
    task = Task.new
    assert_not task.save
  end
end

Use Database Indexes

For production apps, add indexes to frequently queried columns:

add_index :tasks, :completed
add_index :tasks, :created_at

Learn Active Record Queries

Master efficient database queries to avoid N+1 problems:

# Bad - N+1 query
@tasks = Task.all
@tasks.each { |task| puts task.user.name }

# Good - eager loading
@tasks = Task.includes(:user).all
@tasks.each { |task| puts task.user.name }

Moving Beyond Basics

Once you’re comfortable with CRUD operations, explore:

API Development: Rails excels at building APIs with rails new to_do_list_api --api

Action Cable: Real-time features with WebSockets

Active Storage: File uploads and cloud storage integration

Hotwire: Build modern, reactive UIs without writing JavaScript (Turbo + Stimulus)

Background Jobs: Handle asynchronous tasks with Active Job and Sidekiq

Conclusion

Ruby on Rails remains a powerful choice for web development. Its emphasis on developer happiness, combined with mature tooling and a supportive community, makes it ideal for startups and established companies alike.

The framework’s conventions might feel restrictive initially, but they accelerate development once internalized. You spend less time on boilerplate and more time solving actual problems.

Start with simple apps like our to do list, then gradually explore Rails’ deeper features. The learning curve is gentle, but the productivity gains are substantial. Whether you’re building a prototype, an MVP, or a production application, Rails provides the tools and structure to succeed.

The Rails community keeps the framework modern and relevant. With each release, Rails adopts best practices from the wider development ecosystem while maintaining its core philosophy: make developers happy and productive.

Reference Links

  1. Official Rails Guides – https://guides.rubyonrails.org (The most comprehensive resource for learning Rails, maintained by the core team)
  2. Rails API Documentation – https://api.rubyonrails.org (Complete API reference for all Rails versions)
  3. The Odin Project – Rails Course – https://www.theodinproject.com/paths/full-stack-ruby-on-rails (Free, thorough curriculum for learning Rails from scratch)
  4. Exampleshttps://github.com/efernandes-tech/rails-001-ef-to-do-list

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top