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