Skip
Arish's avatar

2. Introduction to Ruby on Rails


What is Ruby on Rails?

Ruby on Rails, often simply called Rails, is a powerful web application framework written in the Ruby programming language. Created by David Heinemeier Hansson in 2004, Rails follows the principle of "Convention over Configuration" - meaning it makes assumptions about what you need, so you can focus on building your app rather than setting up boilerplate code.

Why Choose Rails?

Rails is perfect for building web applications quickly and efficiently. Here's why developers love it:

  • Fast Development: Build features in hours, not days
  • Clean Code: Ruby's elegant syntax makes code readable and maintainable
  • Batteries Included: Everything you need comes built-in
  • Active Community: Thousands of gems (libraries) available
  • Proven at Scale: Used by GitHub, Shopify, Airbnb, and Basecamp

The MVC Architecture

Rails follows the Model-View-Controller (MVC) pattern:

ruby
1# Model - Handles data and business logic
2class User < ApplicationRecord
3  validates :email, presence: true
4end
5
6# Controller - Processes requests and returns responses
7class UsersController < ApplicationController
8  def show
9    @user = User.find(params[:id])
10  end
11end
12
13# View - Displays data to the user (in app/views/users/show.html.erb)

Think of it like a restaurant:

  • Model = The kitchen (prepares the data)
  • Controller = The waiter (takes orders, delivers food)
  • View = The plate presentation (how it looks to the customer)

Rails Philosophy

Rails is built on two main principles:

1. Convention over Configuration (CoC)

Instead of configuring everything manually, Rails uses sensible defaults:

ruby
1# Rails automatically knows:
2# - Model 'User' uses table 'users'
3# - Controller 'UsersController' serves views from 'app/views/users/'
4# - URL '/users' maps to UsersController#index

2. Don't Repeat Yourself (DRY)

Write code once, use it everywhere:

ruby
1# Instead of repeating validation logic everywhere,
2# define it once in your model
3class Article < ApplicationRecord
4  validates :title, presence: true, length: { minimum: 5 }
5  # This validation works everywhere automatically!
6end

What You'll Build

Throughout this course, you'll learn to build complete web applications with:

  • User authentication and authorization
  • Database models with relationships
  • RESTful APIs
  • File uploads with Active Storage
  • Email notifications
  • Background jobs
  • And much more!

Let's get started by setting up your development environment.