Intro to Rails

Reviewed & published by Brayan K

Ruby is a dynamic, beginner-friendly programming language, and Ruby on Rails is its flagship web framework — a full-stack toolkit that turns your Ruby skills into real, database-backed web applications.

Part of the free Ruby course at LearnCodingFast — hands-on lessons with worked examples and the output they print, plus practice exercises and a quick quiz.

By the end of this lesson you'll understand what Rails is, the MVC pattern, convention over configuration, and how a request flows through a Rails app.

What You'll Learn in This Lesson

1️⃣ What Rails Is and How a Project Starts

Rails is a full-stack framework: routing, database access, templating, and security all come included. You scaffold an app with a few terminal commands, and Rails's convention over configuration philosophy means it wires everything together with sensible defaults — leaving you to write the parts that are unique to your app.

# Rails is the most popular Ruby web framework. Install and scaffold from
# the terminal (these are shell commands, not Ruby code):
#
#   gem install rails
#   rails new blog          # creates a whole project folder
#   cd blog
#   rails generate scaffold Post title:string body:text
#   rails db:migrate        # create the database table
#   rails server            # start the app at http://localhost:3000
#
# Rails follows "convention over configuration": sensible defaults mean
# you write very little setup code. The generated app already has:
#   app/models/post.rb        <- the data (Model)
#   app/controllers/...       <- the logic (Controller)
#   app/views/...             <- the HTML (View)

puts "Rails app structure: Model + View + Controller (MVC)"

2️⃣ The MVC Pattern in Action

Rails organises code as Model (data and rules), View (HTML), and Controller (the coordinator). A request hits a route, the controller asks the model for data, and a view renders the result. Notice how every piece is just a Ruby class or template — everything you've already learned.

# These are simplified Rails files showing the MVC pattern.

# 1) MODEL (app/models/post.rb): talks to the database via ActiveRecord.
class Post < ApplicationRecord
  validates :title, presence: true   # a built-in validation
end

# 2) CONTROLLER (app/controllers/posts_controller.rb): handles requests.
class PostsController < ApplicationController
  def index
    @posts = Post.all                # fetch every post (a query!)
  end

  def show
    @post = Post.find(params[:id])   # find one by its id from the URL
  end
end

# 3) ROUTES (config/routes.rb): map URLs to controller actions.
#    Rails.application.routes.draw do
#      resources :posts              # generates all the standard routes
#    end

# 4) VIEW (app/views/posts/index.html.erb): embeds Ruby in HTML with <%= %>.
#    <% @posts.each do |post| %>
#      <h2><%= post.title %></h2>
#    <% end %>

puts "Model fetches data, Controller coordinates, View renders HTML"

Your turn. Plan the MVC pieces for a new resource in plain Ruby. Confirm the TODO format, then run it.

# 🎯 YOUR TURN — a thinking exercise (run with: ruby plan.rb)
# You can't run a full Rails app here, but you CAN plan one in plain Ruby.

# Imagine a "Task" resource for a to-do app.
# 1) List the MVC pieces Rails would generate.
parts = {
  model:      "Task (title:string, done:boolean)",
  controller: "TasksController (index, show, create...)",
  view:       "tasks/index.html.erb"
}

# 2) Print each part of the plan
parts.each do |role, description|
  puts "#{role.to_s.capitalize}: #{description}"   # 👉 TODO: keep this format
end

# 3) Print the generator command you'd run
puts "Command: rails generate scaffold Task title:string done:boolean"

# ✅ Expected output:
#    Model: Task (title:string, done:boolean)
#    Controller: TasksController (index, show, create...)
#    View: tasks/index.html.erb
#    Command: rails generate scaffold Task title:string done:boolean

Mini-Challenge: Model a Resource

A Rails model is a Ruby class with validations. Build a plain-Ruby Article with a valid? check and a summary — the same shape as a real model. Run with ruby resource.rb.

# 🎯 MINI-CHALLENGE: model a resource (plain Ruby stand-in).
# Save as resource.rb and run with: ruby resource.rb
#
# Rails models are Ruby classes. Without Rails installed, model the idea:
# 1. Make a class Article with attr_accessor :title, :published.
# 2. Add a method "valid?" that returns true only if title isn't empty.
# 3. Add a method "summary" returning "<title> (<draft|live>)".
# 4. Create one Article, set values, and print valid? and summary.
#
# ✅ Example output (title "Hello", published true):
#    Valid? true
#    Hello (live)

# your code here

Common Errors (and the fix)

Pro Tips

📋 Quick Reference — Rails

ConceptCommand / Idea
New apprails new blog
Scaffoldrails g scaffold Post ...
Migrate DBrails db:migrate
Run serverrails server
PatternModel / View / Controller
Routesresources :posts

🎉 Lesson Complete!

Practice quiz

What is Ruby on Rails?

  • A Ruby testing library
  • A full-stack web framework written in Ruby
  • A database engine
  • A Ruby version manager

Answer: A full-stack web framework written in Ruby. Rails is a full-stack web framework for building database-backed web apps and APIs.

What does MVC stand for in Rails?

  • Module-View-Class
  • Model-View-Controller
  • Method-Variable-Constant
  • Map-Validate-Convert

Answer: Model-View-Controller. MVC is Model-View-Controller, the pattern Rails organises code around.

Which part of MVC talks to the database via ActiveRecord?

  • The View
  • The Controller
  • The Model
  • The Router

Answer: The Model. The Model represents data and business rules and talks to the database.

What is the role of the Controller?

  • Renders HTML
  • Stores data
  • Receives the request, asks the Model for data, picks a View
  • Defines the database schema

Answer: Receives the request, asks the Model for data, picks a View. The Controller coordinates: it handles the request and chooses what to render.

What does 'convention over configuration' mean?

  • You must configure every file
  • Sensible defaults wire things together so you write little setup
  • Conventions are optional and ignored
  • It only applies to the database

Answer: Sensible defaults wire things together so you write little setup. Following standard naming and structure lets Rails wire things up automatically.

Which command creates a brand-new Rails project?

  • rails server
  • rails new blog
  • rails db:migrate
  • rails generate

Answer: rails new blog. rails new blog scaffolds a whole project folder.

After generating a model, which command creates its database table?

  • rails server
  • rails console
  • rails db:migrate
  • rails new

Answer: rails db:migrate. rails db:migrate applies pending migrations to create the table.

Following conventions, a model named Post maps to which table?

  • Post
  • post
  • posts
  • post_table

Answer: posts. Models are singular (Post) and their tables are plural (posts).

Where are routes that map URLs to controller actions defined?

  • app/models
  • config/routes.rb
  • app/views
  • Gemfile

Answer: config/routes.rb. Routes live in config/routes.rb, often as resources :posts.

Which file type embeds Ruby in HTML using <%= %> tags?

  • .rb files
  • .erb view templates
  • .yml files
  • .json files

Answer: .erb view templates. ERB view templates (.html.erb) embed Ruby inside HTML with <%= %>.

Continue this course

Frequently asked questions

What is Ruby on Rails and what is it for?

Rails is a full-stack web framework written in Ruby for building database-backed web applications and APIs. It packages everything you need — routing, database access, HTML templating, security defaults, and more — so you can go from idea to working app quickly. Rails powered the early growth of companies like GitHub, Shopify, Basecamp, and Airbnb. Its big selling point is developer productivity: a huge amount of common web work is already handled for you by sensible conventions.

What does MVC mean in Rails?

MVC stands for Model-View-Controller, the pattern Rails organises your code around. The Model represents your data and business rules and talks to the database (via ActiveRecord). The View is what the user sees — HTML templates, often .erb files that embed Ruby. The Controller sits in the middle: it receives the web request, asks the Model for data, and chooses which View to render. Separating these three concerns keeps apps organised and makes each part easier to test and change.

What is 'convention over configuration'?

It's Rails's core philosophy: if you follow standard naming and structure, Rails wires things together automatically so you write almost no configuration. Name your model Post and Rails expects a posts table, a PostsController, and views in app/views/posts — all without you specifying it. This dramatically reduces boilerplate and means any Rails developer can find their way around any Rails app. You can override conventions when you need to, but following them is what makes Rails so fast to work in.

Do I need to master plain Ruby before learning Rails?

You should be comfortable with Ruby fundamentals first — variables, methods, blocks, classes, modules, and collections — because Rails is just Ruby with a lot of helpful libraries on top. Models are classes, controllers are classes, and views run embedded Ruby. The good news is that everything in this course (objects, attr_accessor, enumerable methods, gems) maps directly onto Rails concepts. Once the Ruby clicks, Rails becomes learning a framework rather than a new language.