Start a free Courses trial
to watch this video
In this video, we look at how to use the splat (*) in URLs to create completely dynamic URLs. We also look at how to pass requests from one route handler to another.
This video doesn't have any notes.
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up[?mellow guitar music?] 0:00 [Think Vitamin Membership - Est. 2010] [membership.thinkvitamin.com] 0:03 [Ruby - Sinatra Dynamic URLs with Jim Hoskins] 0:07 In the previous video, we saw how to embed variables into our url's. 0:13 Now we're going to take a look at how to create more dynamic url's in Sinatra. 0:16 In the last video, we saw how to create dynamic url's by adding variables 0:20 into the route names. 0:24 Like in this example here, we have /blog/post/ 0:25 :with the variable id embedded into the url. 0:28 Then we're able to access that through the params (:id) object. 0:32 In this case, we're just looking up the post by the id and returning it. 0:36 But sometimes the dynamic portions of our urls are a little more complicated 0:40 than can be handled with a simple pattern like this. 0:44 That's where splats come in handy. 0:47 To use a splat in a url, you simply use an asterisk. 0:49 So let's create the most generic url that we can. 0:53 We'll do get, and we'll do a forward slash and then we'll put a splat or an asterisk /* 0:56 and then we'll define our block. 1:02 Now, when the asterisk is used in a route, 1:05 what it does is it basically catches everything, including letters, numbers, 1:08 slashes, or really any character. 1:12 It's saying "Whatever it is, we'll accept that route." 1:14 So by doing a slash and a star /* 1:17 we're saying that literally any route that comes through would match this pattern. 1:19 We can actually get the value of the dynamic part of the url 1:23 that is caught by the splat by loading params splat. 1:27 So we can inspect it by loading up params and we'll put [:splat] 1:30 and just so we can see it in our browser, I'm going to call the .inspect method 1:37 and what inspect does in Ruby is any object should have the inspect method 1:42 and when you call it, it will return a human-readable form of that object. 1:46 So when we flip over to our code, and let's just type in something completely random, 1:51 so let's just say random. 1:56 We'll see that the splat parameter is an array with the string ['random']. 1:59 So let's put some other parts on here-- more/stuff, 2:04 and we'll click okay. 2:10 And so we can see that everything--including the slashes 2:12 and all the different parts--are caught into this one string inside of this array. 2:15 So from there, we could break that apart or inspect it, or try to match it 2:19 using any other more advanced type of matching 2:23 to figure out what to do with this url. 2:25 Now, you'll notice that it's not just a string--it's an array with a string in it, 2:29 and that's because you could have multiple splats inside of your url. 2:33 So for instance, if we had a /* and then maybe something like /static/ 2:36 and then another /splat. 2:43 What this would do is it would match any route with anything, 2:46 so long as /static/ appeared somewhre in it. 2:49 So if we modify our url to have /static/some/more/stuff, 2:52 we'll see that it broke up our url around this /static/ part 3:03 because it already matched it, so our first splat is random/more/stuff 3:07 and then part 2 is some/more/stuff. 3:12 So you can have multiple splats around pieces of static parts of your url. 3:14 I'm going to go ahead and just comment this out, 3:19 and let's look at how this might be used. 3:23 So for instance we have this get/blog/post/id 3:25 and any url for a post should just have this one variable here. 3:29 But let's say I don't find the correct post based on that id, 3:34 we want to show a special page, maybe showing similar posts 3:37 or guessing at what posts they would want. 3:41 Well, in Sinatra, you can actually have multiple routes 3:43 that would match these same urls. 3:46 So for example, we could write this route: 3:48 get "/blog/post and then put our splat to match anything that's like blog/post. 3:50 And then, we'll create our block, 3:56 and let's just paste in this string here. 3:58 and say "We couldn't find, and then just print out the splat. 4:01 Obviously, you would want to do something a little more useful than that. 4:05 So how would we actually get this to trigger? 4:08 Well, when it receives the request, it will go down all the routes in order 4:11 and try to match it. 4:13 So if we gave it the url /blog/post/foo, 4:15 it would first try this route and then it would actually execute it, 4:22 so let's say params[:id] would be foo. 4:28 And in our find/post/ method if our slug here is foo, then we return this content here. 4:31 Otherwise it will return nil. 4:38 Now, let's say that we passed in something else, like /not/a/real/post. 4:41 In this case, it wouldn't match this url because it has multiple slashes 4:49 and the id wouldn't actually be acceptable for that, 4:53 but it would for this url, since it's a splat. 4:56 But let's say instead of an unrealistic url, we gave it bar, 4:59 which is a perfectly acceptable url by this pattern, but it may not exist. 5:04 So in this case what we can do is use a special method called pass() 5:09 and what pass will do is say that this route 5:14 is not the correct route for these circumstances, 5:17 and it will actually stop execution right when it's called 5:20 and then it will start looking for any other routes that would match. 5:23 So if we called pass() right here, 5:27 it would always just pass down to this next one, 5:29 so we want to put some conditional logic on it. 5:31 So in this case, we're going to say pass unless/ @post 5:33 and unless is just like "if" but the opposite of it, so the pass will execute 5:37 if post is false or nil or some other falsey value. 5:43 So in this case if we nip pass/bar, @ post will be nil and pass will be called, 5:47 and all this code down here--line 13--would be skipped and we look for the next url. 5:56 So let's try that out. 6:00 So let's go to /blog/post/foo, 6:02 and we could see our post content. 6:07 But if we go instead to /blog/post/bar, 6:09 you could see we couldn't find bar. 6:14 so if we have multiple slashes, like baz, the splat will actually pick it up. 6:17 So the splat helps us in this case 6:24 in that we can actually handle urls much more dynamically. 6:25 In the next video, 6:28 we'll take a look at handling 404 and other special cases in Sinatra. 6:29 [?mellow guitar music?] 6:34 [Think Vitamin Membership - Est. 2010] [membership.thinkvitamin.com] 6:36
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up