Understanding Rails · Season 1: The trip of one click
What happens when you click
Episode 1 · overview · about 3 minutes · Rails 8.1 · by Tommy A. Caruso
How was this episode?
The series' author reads every note, and no one else sees it. When something is wrong or unclear, it gets fixed and the episode re-cut. How this series stays right.
Not endorsed yet. Are you a Rails developer? What's an endorsement?
Endorse this version To endorse: create an account, confirm your email, then link GitHub.
The cold open, the map of the trip, and HTTP: requests, responses, verbs, status codes, cookies.
Free to watch. If it helped, support the series.
Transcript
0:00 The click
This is Switchback, a small Rails app where hikers leave notes about the trails they just walked.
Someone just finished Eagle Ridge, and wants to warn the next hiker. Mud past the second bridge. Bring gaiters.
They click Post note. And there it is, at the top of the list. The page didn't reload. Nothing flickered.
It took about as long as a blink. So, what just happened?
0:31 The map of the trip
Here's the trip that click took. Out of the browser, across the internet, and into a server. Then through Rails: the router, a controller, a model that talks to the database, and a view. And all the way back.
By the end of this video, every stop on this map will make sense. We'll follow this one click the whole way. And we start where every web app starts: with HTTP.
0:56 HTTP
Part one. HTTP, the language browsers and servers speak.
HTTP is a conversation that takes turns. The browser sends a request. The server sends back a response. One question, one answer.
1:12 The request, as text
And the conversation is just text. Here's the request our click sent.
The first line says what we want. Post is the verb: we're sending something new. Then the path says where it goes: the notes for trail forty two.
Next come the headers, short notes about the request. Which site it's for. What kinds of answer the browser will take. And a cookie. Hold on to that one.
Last comes the body: the note itself, and a hidden token that proves the form came from Switchback's own page.
1:47 Verbs
Our click used post, one of a handful of verbs. Get reads. Post creates. Patch updates. Delete removes. Rails builds a lot on these four, as we'll see at the router.
1:59 Status codes
Then the server answers. A response starts with a status code. Two hundreds mean it worked. Three hundreds mean look somewhere else. Four hundreds mean the request was wrong. Five hundreds mean the server broke.
2:14 The response
Then come its own headers, and a body. Usually the body is a whole page of HTML. For our click, it was something smaller: just the new note, and where to put it. That's Hotwire at work, and we'll get there.
2:28 Stateless, and the cookie
Now the strange part. HTTP forgets. Every request arrives as a stranger. The server keeps no memory of the one before.
So how does Switchback know who wrote the note? The cookie. When you signed in, the server gave your browser a small signed token. The browser sends it back with every request, and Rails reads it to know it's you.
2:51 Recap
So that's HTTP. A request carries a verb, a path, headers and a body. A response carries a status, headers and a body. Every time, from scratch, with a cookie to say who you are.
Our request has crossed the internet and reached the server. Who answers the door? That's next.
Check your understanding
-
A server answers with status 302. What is it telling the browser?
- It worked, here is the page
- Look somewhere else
- Your request was wrong
- The server broke
Show answer
Look somewhere else. Codes from 300 to 399 are redirection messages. They point the browser to another place. Source: HTTP response status codes (MDN).
-
The browser sends two requests to Switchback, one after the other. What does HTTP itself remember about the first one when the second arrives?
- The path of the first request
- Who sent it, as long as it is the same browser
- The body of the first request
- Nothing: each request arrives on its own
Show answer
Nothing: each request arrives on its own. HTTP is stateless. There is no link between two requests, so each one has to carry what the server needs to know. Source: HTTP is stateless, but not sessionless, Overview of HTTP (MDN).
-
You signed in earlier. How does Switchback know the note you just posted came from you?
- Your browser sends the cookie the server gave it back with the request
- The server remembers your last request
- The note body includes your name
- The router reads your IP address
Show answer
Your browser sends the cookie the server gave it back with the request. A cookie is a small piece of data the server sends to the browser. The browser sends it back with later requests. Source: Using HTTP cookies (MDN).
-
A hiker wants to fix a typo in a note that already exists. Which verb fits that request?
- GET
- POST
- PATCH
- DELETE
Show answer
PATCH. PATCH changes part of something that already exists. POST creates something new. Source: PATCH, HTTP request methods (MDN).