Ruby on Rails Tutorial
Learn Ruby on Rails step by step: 37 lessons in 9 chapters with examples, quizzes, diagrams and projects.
Start learning →
of lessons completed
Getting Started
- What Is Ruby on Rails? Learn what Ruby on Rails is, why it is called a full-stack framework, how MVC and convention over configuration work, and build a tiny MVC app in plain Ruby.
- Your First Rails App Install Rails, run rails new, start the server and tour the folders of a new app. Includes a runnable Ruby stand-in that scaffolds a project tree so you see what the generator does.
- The Rails Request Cycle Follow one HTTP request through Rack, middleware, the Rails router, a controller action and a view. Build a runnable Rack-style app with middleware in plain Ruby to see how the pieces connect.
- Rails Conventions and Naming Learn the Rails naming rules for models, tables, controllers and files, how inflections pluralize words, and how Zeitwerk autoloading finds classes. Includes runnable Ruby stand-ins.
- Environments and Configuration Understand Rails environments (development, test, production), database.yml, initializers, encrypted credentials and environment variables, with a runnable YAML and ERB configuration stand-in.
Routing and Controllers
- Routing Basics Learn how config/routes.rb maps URLs to controller actions in Rails: get and post routes, dynamic segments, constraints, root, redirects and named route helpers, with a runnable matcher stand-in.
- Resourceful Routes Learn Rails resourceful routing: what resources generates, the seven RESTful actions, singular resources, nested routes, member and collection routes, namespaces and concerns, with a runnable expander.
- Controllers and Actions Learn how Rails controllers work: actions, instance variables, params, render, redirect_to, status codes, respond formats and the standard seven-action pattern, with runnable plain-Ruby stand-ins.
- Strong Parameters and Filters Learn Rails strong parameters (require, permit, expect) to prevent mass assignment attacks, plus before_action, after_action and around_action filters and rescue_from, with runnable plain-Ruby stand-ins.
- Sessions, Cookies and Flash Learn how HTTP stays stateless, and how Rails uses cookies, the encrypted session and the flash to remember users and show one-time messages. Includes runnable stand-ins for each mechanism.
Views and Templates
- ERB Templates in Rails Learn how Rails views use ERB: the three main tags, loops and conditionals, automatic HTML escaping, how controller instance variables reach templates, and the file naming rules. Runnable ERB examples included.
- Layouts and Partials Learn how Rails layouts wrap every page with yield, how partials share view fragments with locals and collections, and how content_for fills named regions. Includes a runnable layout and partial engine.
- View Helpers Learn Rails view helpers: link_to, content_tag, tag attributes, text and number helpers, and how to write your own helper methods. Includes a runnable helper stand-in with safe escaping.
- Hotwire: Turbo and Stimulus Learn Hotwire in Rails: Turbo Drive, Turbo Frames, Turbo Streams and Stimulus controllers, with read-only Rails code and runnable Ruby stand-ins for frames and stream messages.
Active Record Models
- Active Record: Models and Databases Learn what Active Record is in Rails: how a model class maps to a table, rows become objects, columns become attributes, and conventions connect them. Includes a runnable mini ORM stand-in.
- Migrations and the Schema Learn Rails migrations: create tables, add columns and indexes, run and roll back changes, and what schema.rb is for. Includes a runnable migration runner stand-in with versions and rollback.
- CRUD with Active Record Learn the Active Record CRUD methods: new, create, save, find, update, destroy, bang methods and change tracking. Includes a runnable stand-in with timestamps, dirty tracking and errors.
- Attributes, Types and Enums Learn how Active Record casts values into Ruby types, how to declare enums, defaults and the attributes API, and how to store JSON and secure data. Includes a runnable enum and type-cast stand-in.
Associations and Queries
- Associations - belongs_to and has_many Learn how Rails models relate to each other with belongs_to, has_many and has_one, foreign keys, dependent options and inverse_of, with runnable Ruby stand-ins that show how the readers work.
- Many-to-Many and Polymorphic Associations Model many-to-many links with has_many :through and join models, understand has_and_belongs_to_many, and share one table across models with polymorphic associations, using runnable Ruby stand-ins.
- The Active Record Query Interface and Scopes Master Active Record queries: where, order, limit, select, pluck, group, joins, find_by, exists?, safe SQL placeholders and named scopes, with a runnable lazy-relation stand-in that shows how chaining works.
- Eager Loading and the N+1 Query Problem Find and fix the Rails N+1 query problem with includes, preload and eager_load, strict_loading and counter caches, with a runnable query counter that shows 101 queries shrink to 2.
Forms, Validation and Callbacks
- Forms with form_with Build Rails forms with form_with, model-backed fields, labels, selects, checkboxes, nested attributes and file uploads. Understand the HTML it generates, the CSRF token and the hidden method field.
- Strong Parameters and Mass Assignment Understand mass assignment attacks and how Rails strong parameters (require, permit, params.expect) whitelist fields, with nested arrays, unpermitted parameters and a runnable stand-in of the whitelist logic.
- Active Record Validations Validate Rails models with presence, length, format, numericality, uniqueness, inclusion and confirmation, write custom validators, read errors and show them in forms. Includes runnable stand-ins.
- Callbacks and Form Objects Learn Active Record callbacks (before_validation, before_save, after_commit and more), their order, the traps of side effects, and how form objects with ActiveModel keep complex forms clean. Runnable stand-ins.
Authentication and Security
- Authentication with has_secure_password Build login in Rails with has_secure_password and bcrypt, understand password hashing and salting, authenticate_by, the Rails 8 authentication generator, and password reset tokens. Includes a runnable hashing stand-in.
- Sessions, Cookies and the Current User Learn how Rails sessions and cookies keep users logged in, how encrypted cookie sessions work, flash messages, remember-me cookies, current_user helpers, before_action guards and secure cookie settings.
- Web Security and Authorization in Rails Protect a Rails app from CSRF, XSS, SQL injection, mass assignment and insecure direct object references, set security headers, and add authorization with policies. Includes runnable escaping and policy stand-ins.
- API Authentication with Tokens Build a token-protected JSON API in Rails: api-only mode, bearer tokens, hashed token storage, authenticate_with_http_token, rate limiting and CORS. Includes runnable Ruby stand-ins for token issuing and checking.
Testing, Jobs and Deployment
- Testing Rails with Minitest Learn how Rails tests work: Minitest basics, assertions, setup and teardown, model tests, fixtures, running tests with bin/rails test, and the testing pyramid. Includes runnable Minitest examples.
- Controller, Integration and System Tests Test Rails controllers and whole user flows with ActionDispatch::IntegrationTest and Capybara system tests. Learn assertions for responses, redirects and DOM, test doubles and a runnable Rack app test stand-in.
- Background Jobs and Caching Move slow work out of requests with Active Job and queue backends, retries and idempotency, then speed up pages with Rails.cache, fragment caching and cache keys. Includes runnable queue and cache stand-ins.
- Mailers and File Uploads with Active Storage Send email with Action Mailer, preview it, and store uploads with Active Storage: attachments, variants, direct uploads, validation and cloud services. Includes runnable ERB mail and blob-store stand-ins.
- Deploying a Rails App to Production Deploy a Rails app: production configuration, credentials and secrets, asset pipeline, database migrations, Docker and Kamal, health checks, logging and a launch checklist. Includes a runnable environment-config stand-in.
Projects and Practice
- Project: Build a Blog with Rails Build a complete Rails blog: models, routes, controllers, views, comments, authentication, tests and deployment steps. Includes a runnable Ruby stand-in of the domain model, scopes and validations.
- Rails Practice Exercises and Review Practice Rails with graded exercises, runnable Ruby solutions, a debugging checklist, a review of the whole course and common Rails interview questions with short answers. Finish with a plan for what to learn next.