Locked lesson.
About this lesson
Exercise files
Download this lesson’s related exercise files.
Understanding Routing.docx58.7 KB Understanding Routing - Solution.docx
58.6 KB
Quick reference
Understanding Routing
In this video we'll be discussing the basics of Routing in Rails.
When to use
Any time you create a web page, you need to set up a route to that web page (either automatically using the Rails Generator, or manually).
Instructions
When using the Rails Generator, your route will be created automatically in the /config/routes.rb file. For our first index web page, that route will look like this:
get 'home/index'
To change the route of that page to make it our root homepage, change the above line of code to this:
root 'home#index'
That creates a "Root Route".
Remember that you can check your routes in the terminal at any time by using the "Rake Routes" command.
Hints & tips
- Every webpage needs a route
- Routes are found in the /config/routes.rb file
- A basic route looks like this: get 'home/index'
- A root route looks like this: root 'home#index'
- To view your current routes use this command in the terminal: rake routes
- 00:04 In this video, I wanna talk a little more about routing.
- 00:07 I wanna get a little bit more of an understanding of routes and
- 00:10 what's going on here.
- 00:11 And we also wanna change our app a little bit.
- 00:14 In the last video, we created this web page, and the URL is home/index.
- 00:20 And if we go back to our actual homepage,
- 00:22 we still have this weird welcome aboard screen.
- 00:26 We really don't want that, what we want is this to be our new homepage.
- 00:29 So how do we do that?
- 00:30 Well, it all gets tied into routing.
- 00:33 So right now, this is our routes.rb file, it's in the config directory,
- 00:37 we had this open in the last video.
- 00:39 And this is the index that Rails generated for us when we used the generator.
- 00:44 But we can change that.
- 00:45 What we want is to make this page our root page.
- 00:49 And a root is just, whenever you type in the basic URL of your website,
- 00:55 that's the root URL.
- 00:57 So we need to create a root route, kinda hard to say, but it's pretty easy.
- 01:02 Just come here, and we're gonna do this manually 'cause it's quick and easy.
- 01:05 We type in root, and then it's just like before,
- 01:10 home/index except for this slash,
- 01:13 instead of a slash, it's a number sign or a hashtag.
- 01:18 So if we save this file, come back here and hit reload, boom, there we go.
- 01:23 And now check this out, if we type in home/index,
- 01:27 this page still exists at home/index because this route is still there.
- 01:32 In fact, we can get rid of this.
- 01:34 I'm just gonna type a hashtag to comment that out.
- 01:37 And if we save this and come back, and you see we're at the home/index URL,
- 01:42 hit reload, we get an error because we've removed that route.
- 01:47 Now, the web page still exists, it's right here.
- 01:49 It's still in our views > home directory, but
- 01:54 our app can't find it because we've removed the route.
- 02:00 And like I said, routing can get pretty complicated.
- 02:04 And for the most part, you don't need to learn the complicated routing stuff,
- 02:09 especially not for beginners type things and even intermediate Rails programing.
- 02:13 You're not gonna use routing a whole lot, it's just gonna be basic get and
- 02:18 root routes, like this.
- 02:19 So it's not something you have to overly worry about.
- 02:23 But if you do, I mentioned in the last video, that Rails guide.
- 02:27 I love these Rails guides.
- 02:29 You're gonna spend a lot of time going through these throughout your lives.
- 02:32 And let's see, Rails Routing from the Outside In, here it is.
- 02:35 Just all kinds of stuff you can learn about it.
- 02:37 But the root route, we're well on our way.
- 02:39 Now, here's one more tip, when it comes to routing.
- 02:43 You can figure out what all your routes are currently by going to your routes.rb
- 02:47 file and just looking at it.
- 02:49 But sometimes it's kinda hard to read this file.
- 02:51 Also, I should mention there's some information in the file itself,
- 02:55 different examples.
- 02:56 So kinda look through here, just eyeball this stuff, and
- 02:58 you'll get an idea of different things you can do with routing.
- 03:01 But if you come down to your command line terminal and type in rake routes,
- 03:05 what that does it goes through this routes file and
- 03:08 it creates a list of all the different routes that you have in your app.
- 03:12 Now, right now all we have is this root, and
- 03:14 you can see it goes to this forward slash 'cause it's our root route.
- 03:18 It used to be home/index, now it's just /nothing.
- 03:24 So that's why that little slash guy is there.
- 03:27 But as our app gets more complicated, this will grow.
- 03:30 And sometimes we'll need to know how the routing works, and so
- 03:33 we'll use this rake routes command.
- 03:35 So keep that in mind in the future.
Lesson notes are only available for subscribers.