Rails in one lesson

Models views controllers

Rails splits every feature into three roles with fixed homes. Models hold data and rules, views render pages, and controllers connect the two.

The sketch below shows where one todo feature lives inside a fresh application.

# app models todo file holds the model # app views todos folder holds the pages # app controllers todos file holds the actions

Routes

Routes map URLs to controller actions in one central file. One resources line creates the seven RESTful routes: list, new, create, show, edit, update and delete.

Read the routes file as the table of contents for the whole application.

Rails.application.routes.draw do resources :todos root "todos#index" end

ActiveRecord sketch

ActiveRecord models look like plain Ruby classes with database powers mixed in. The sketch below runs without any database at all.

Learn the shape first with plain attributes, then let Rails add persistence, validations, and relations later.

class Todo attr_accessor :title, :done def initialize(title) @title = title @done = false end end

How a Rails app starts

A new Rails application starts from two typed commands. Install the rails gem once, then generate a fresh project folder.

Nothing below boots an application. Treat this lesson as a map, then build the two capstones with plain Ruby first.

Install the gem

gem install rails

39 gems installed

Check the version

rails -v

Rails 8.1.4.1

Generate the application

rails new blog

create config/recurring.yml create db/queue_schema.rb create bin/jobs gsub config/environments/production.rb create db/cable_schema.rb force config/cable.yml

Look at what was generated

ls blog

Dockerfile Gemfile Gemfile.lock README.md Rakefile app bin config config.ru db lib log public script storage test tmp vendor

The output above comes from the official ruby:3 image, which has the build tools Rails needs. On a bare machine the same commands work after a compiler and the usual headers are installed.

Questions

A new set of questions in a new order every time. Choose one answer for each, then check.

  1. Which role does each part of MVC take?

    Answer

    Models hold data and rules, views render pages, controllers connect the two

    A model is the data and its rules, a view draws the page, and a controller takes the request and calls both.

  2. What does this program print? class Todo attr_accessor :title, :done def initialize(title) @title = title @done = false end end todo = Todo.new("write tests") puts todo.title puts todo.done

    Answer

    write tests / false

    The constructor stores the title and sets done to false, and puts prints both values plainly.

  3. What does this program print? class Todo attr_accessor :title def initialize(title) @title = title end end todo = Todo.new("one") todo.title = "two" puts todo.title

    Answer

    two

    attr_accessor writes both the reader and the writer, so the second value replaces the first.

  4. What does this program print? class Todo attr_accessor :title, :done def initialize(title) @title = title @done = false end def done? @done end end puts Todo.new("ship").done?

    Answer

    false

    The constructor sets the instance variable to false, and the query method returns it unchanged.

  5. In which file does a Rails application keep its routes?

    Answer

    config/routes.rb

    The route table is one file under config, which is why the lesson calls it the table of contents of the application.

  6. What does resources :todos add to the route table?

    Answer

    Seven RESTful routes for the todo resource

    The resources line writes index, new, create, show, edit, update and destroy in one call.

  7. What does root "todos#index" mean?

    Answer

    The index action of the todos controller answers the site root

    The string names a controller and an action, and root is the path that both map to.

  8. What does rails new blog do?

    Answer

    Creates a folder with a fresh Rails application in it

    The generator writes the skeleton of an application, and the next command starts it.

  9. What does this program print? class Todo attr_accessor :title, :done end todo = Todo.new todo.title = "later" p todo.title

    Answer

    "later"

    A class without initialize still constructs, and p prints the string with its quotes.

  10. What does ActiveRecord add to a plain Ruby class like the Todo sketch?

    Answer

    Database persistence, validations and relations

    The plain sketch shows the shape; the framework is what adds the database and the rules around it.

Article author: Arthur Isaev