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 full set of list, show, create, update, and delete paths.
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.
Next: Net::HTTP in depth
Article author: Arthur Isaev