Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Start a free Basic trial
to watch this video
We've set up our posts resource, and we're able to view posts in our browser. But what's actually happening when you load that URL?
Here's an overview of how Rails handles a request.
- Rails looks at the request, to figure out which code should handle it.
- The request gets routed to an action method on a "controller".
- The controller loads the resource in from database using a "model class".
- The controller renders a "view" using the model data, to generate the response content.
To retrieve the URL "http://localhost:3000/posts", your browser sends an HTTP "GET" request.
- Your browser uses a communication standard called HyperText Transfer Protocol, or HTTP, to talk to web servers. That's the "http:" at the start of the URL.
- "localhost:3000" is the host and port.
- "/posts" is the path to the resource you want.
When you press Enter to load the URL, your browser sends an HTTP GET request - it gets the page. (There are other HTTP request types like POST and DELETE, too. We'll look at those later.) If you'd like to learn more about the HTTP protocol, check out the HTTP Basics course.
If we switch to the terminal and look in the log, we can see where Rails received the request, and how it was handled.
Started GET "/posts" for ::1 at 2016-05-28 12:37:31 -0700
Processing by PostsController#index as HTML
Rendering posts/index.html.erb within layouts/application
Post Load (0.1ms) SELECT "posts".* FROM "posts"
Rendered posts/index.html.erb within layouts/application (1.6ms)
Completed 200 OK in 23ms (Views: 20.2ms | ActiveRecord: 0.1ms)
This log shows where Rails recieved the request, passed it to a controller, loaded some model data, and rendered a view.
-
0:00
[MUSIC]
-
0:04
We've set up our post resource and we're able to view posts in our browser.
-
0:08
But what's actually happening when you load that URL?
-
0:11
Your browser sends a request for the post resource and the web framework, Rails,
-
0:16
has to send back a response with some HTML for the browser to display.
-
0:21
Here's an overview of how Rails processes a request.
-
0:24
Rails looks at the request to figure out which code should handle it.
-
0:28
The request gets routed to action method on a controller.
-
0:32
The controller loads the resource in from the database using a model class.
-
0:36
Then the controller renders a view using the model data
-
0:39
to generate the response content.
-
0:41
To retrieve the URL http://local host:3000/posts,
-
0:46
your browser sends an HTTP get request.
-
0:50
Your browser uses a communication standard called Hyper Text Transfer Protocol or
-
0:55
HTTP to talk to web servers.
-
0:58
That's the HTTP at the start of the URL.
-
1:00
Localhost:3000 is the host and the port.
-
1:05
And /posts at the end is the path of the resource you want.
-
1:09
When you press enter to load the URL,
-
1:11
your browser sends an HTTP get request, it gets the page.
-
1:16
There are other HTTP requests types like post and
-
1:19
delete too, we'll look at those later.
-
1:22
If you'd like to learn more about the HTTP protocol, just check the teacher's notes.
-
1:27
If we switch to the terminal and look in the log,
-
1:30
we can see where Rails receives the request and how it was handled.
-
1:33
[SOUND] This log shows where Rails received the request,
-
1:37
passed it to a controller, loaded some model data and rendered a view.
-
1:41
Over the next few videos, we'll look at each of these components.
-
1:45
Right now we just want to give you an overview.
-
1:47
So don't worry about remembering every detail you see.
You need to sign up for Treehouse in order to download course files.
Sign up