URL Parameters4:19 with Jay McGavren
We want to be able to type in "/Nick Pettit" or "/Robots" or any other page we've created a text file for, and have it load. But there's no way we can create separate Sinatra routes for all those. Not only would our app code be really long, we can't possibly anticipate every page that users might want to create. So instead, we're going to create a single route that will match any page users might attempt to visit. And we'll do it using a "URL parameter".
We're set up to load page content from text files. 0:00 Next we need to display that content in the browser. 0:03 We want people to be able to type in a path and 0:06 see the contents of a text file that was loaded in from the pages directory. 0:08 Let's add directions to the welcome paid so that people know to try this, 0:13 we'll let it welcomed the RB in the views directory and 0:18 will add in an age female paragraph with our directions Visit 0:22 /PageName, To view a page. 0:27 Save that, and now if we run the app. 0:35 And reload the route path, we'll see those directions. 0:41 But if we try that now by clicking in the address bar and typing in Nick Pettit. 0:45 Remember that's the name of the sample text file we created. 0:53 We see Sinatra doesn't know that steady. 0:56 By the way notice that the space in the URL gets replaced with a %20. 0:59 Space characters aren't valid in a URL so the browser encodes them. 1:04 Sinatra still gets the space character in the browser requests though 1:08 we want to be able to type in slash Nick Pettit or slash robots or 1:12 any other page we've created the text file for and have it load, but 1:16 there's no way we can create separate Sinatra outs for all those. 1:19 Not only would our app code be really long, 1:23 we can't possibly anticipate every page that users might want to create. 1:25 So instead, we're going to create a single route that will match 1:30 any page users might attempt to visit. 1:32 And we'll do it using a URL parameter. 1:35 A URL parameter allows users to substitute any string they want for 1:38 part of the request path. 1:42 That string will then be accessible within your Sinatra route. 1:44 First let's set up a route with a URL parameter. 1:47 We want this to respond to HTTP get requests. 1:50 So we'll use the get method as we did before. 1:53 So we'll go here into wiki.rb then copy get method. 1:58 Then we need a path to match. 2:03 Paths always start with the slash. 2:05 So we'll set that up in a string. 2:07 The next segment of the path will be our URL parameter. 2:10 Parameters are marked with a colon followed by a parameter name. 2:13 We want this parameter to be the title of the page we want to view. 2:16 So, we'll call it title, :title. 2:20 And as always the route needs a block to call when it gets a matching request. 2:25 Let's set the route up so that we can look at the parameter we're getting 2:32 from the URL parameters gets stored along with other parameters in the params hash. 2:35 You can access params within any Sinatra route block. 2:41 The keys of the params hash are the names of the parameters you can use either 2:44 a symbol or a string whichever you prefer both will access the same parameter. 2:48 So to access the title parameter we give it the symbol, title as a key. 2:53 Our title URL parameter is now the return value of the block. 2:59 So that's what will be returned in the response to the browser. 3:02 Let's save our work, restart our app. 3:06 And then reload the preview for the /NickPettit URL. 3:12 We can see that it takes the string from the request path 3:17 along with the encoded space character. 3:20 Puts it into the title URL parameter and then prints that parameter out. 3:22 Now that we've got a page title, we can pass it to the page content method to load 3:27 text files from the pages directly. 3:31 So, instead of just returning the title parameter, we'll pass it to page content 3:34 as an argument, page_content (params(:title): will be our new argument. 3:38 And since that's the last expression on the block, 3:47 the page text will be the new return value. 3:49 Let's restart the app again. 3:52 And reload our preview page. 3:57 Instead of the Nick Pettit title parameter we'll see the content 4:00 of the text from the page's directory. 4:03 This page is a little bare bones though. 4:06 We're just dumping raw text in the response instead of HTML. 4:08 We're not displaying the page title anywhere. 4:11 So, up next, we'll show you how to use ZR beat to embed the title and 4:14 text into an HTML template. 4:17
You need to sign up for Treehouse in order to download course files.Sign up