The Root Path2:39 with Jay McGavren
Let's add a welcome page that's always available for new visitors to the site. Remember how, in the last stage, Sinatra failed to route our request unless we added a path? If a URL doesn't contain a path, browsers will request the root path by default.
[MUSIC] 0:00 Over the rest of this course will be building a wiki app. 0:05 Wikis, if you're not familiar, are websites that allow visitors to add their 0:08 own pages which others can view later. 0:11 Our wiki's going to load pages from simple text files that are saved in your project 0:14 directory. 0:17 I'm going to create a new workspace. 0:20 I'll name it wiki, and I'll be sure to choose the Ruby environment. 0:22 If you prefer you can just go to this video's page and launch workspace. 0:30 It's based on mine and the environment will be set up for you. 0:33 We'll give it a moment to load. 0:37 Now let's create a new file to hold all of our app's Ruby code. 0:39 Since this is a simple project we can just use one file to hold everything. 0:42 We'll call it wiki.rb. 0:47 The Sinatra gem is already installed in workspaces so 0:52 we don't need to install it, but we do need to load it. 0:55 So, at the top of our file, we'll type require "sinatra". 0:57 And because the app won't be running on our local computers we need 1:04 to be sure to add set :bind, "0.0.0.0". 1:11 Let's add a welcome page that's always available for new visitors to the site. 1:16 Remember how in the last stage, Sinatra failed to route our request unless 1:22 we added the path like slash apples slash banana etc? 1:26 If a URL doesn't contain a path, browsers will request the root path by default. 1:30 The root path is just a single slash, so if we make a Sinatra route consisting of 1:35 a single slash, it'll match URLs without an explicit path. 1:39 So let's create one and place our welcome HTML there. 1:43 We want to match get requests, so we'll call the Sinatra get method and 1:46 we'll give it a path of just a plain slash all by itself. 1:49 Then we need a block. 1:53 And we're gonna have that block return a simple string. 1:55 With an HTML level one heading. 1:58 Welcome to our Wiki. 2:01 Close the level one heading and end this string. 2:06 Now let's go into the console and run the file we just created. 2:10 So we'll type ruby wiki.rb. 2:13 Then we can go to the preview menu and choose port 4567, 2:18 since it's running on port 4567. 2:23 And the preview window will launch because we set up a route for the root path, 2:26 even though we didn't add a path on to the URL, our welcome HTML still loads. 2:31 This will now be the first thing users see when they visit our site. 2:36
You need to sign up for Treehouse in order to download course files.Sign up