Rails in one lesson
| Models views controllers | |
| Routes | |
| ActiveRecord sketch | |
| How a Rails app starts |
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.
gem install rails
rails new blog
Next: Capstone: todo CLI
Article author: Arthur Isaev