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 already have a route to accept GET requests for one path, and return some HTML in the response. We can support additional paths by adding additional routes, one after another.
-
0:00
Here's a workspace with our same Sinatra app from before.
-
0:03
Notice I made sure to add set bind 0, 0, 0, 0 right after
-
0:07
the require Sinatra lines so that I can connect to the app from a remote computer.
-
0:13
We already have a route to accept get requests for the /apple path and
-
0:17
returns to main HTML in the response.
-
0:20
We can support additional paths by adding additional routes, one after another.
-
0:25
So let's go down here to the bottom,
-
0:28
we'll add another get route for a path of /banana.
-
0:35
We'll give it a block which will be used to determine Sinatra's response.
-
0:41
Now let's go up here and make this a level one heading, while I'm thinking about it.
-
0:47
And we'll do the same here, level one heading.
-
0:52
Here's a ripe banana, close our heading, and close our string.
-
0:59
And then we'll set up a third route.
-
1:02
Also for get request, and we'll give this one a path of /carrot.
-
1:09
Set up a block.
-
1:13
Level one heading.
-
1:16
Here's a crunchy carrot.
-
1:26
We'll be sure to save that, and then we need to go to the console and run it.
-
1:31
Ruby produce.rb.
-
1:35
Now we can go into the preview and reload, and there's our /apple path.
-
1:42
And now we can change the path from /apple to /banana.
-
1:49
And a get request for that path will be sent instead.
-
1:52
When a request comes into a Sinatra app,
-
1:54
it looks through the routes you've defined one by one from top to bottom.
-
1:59
The first routes match both the request method, get in this case,
-
2:02
and the resource path will be chosen, and the accompanying Ruby block will be run.
-
2:07
So this request will match the GET /banana route in our app, the block for
-
2:11
that route will be run, and we'll get different HTML in response.
-
2:15
We can also change the path to /carrot,
-
2:20
Which will match the third route and run the third block.
-
2:24
For additional practice you should try adding more get routes to our
-
2:27
produce.rb app now.
-
2:29
You can also try returning different HTML strings from the existing routes.
-
2:34
We'll see you in the next stage where we'll be talking about a Sinatra feature
-
2:37
that will let you render not just short strings but an entire HTML page.
You need to sign up for Treehouse in order to download course files.
Sign up