Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Well done!
      You have completed Rails 8 Course - Beginner to Intermediate!
      
    
You have completed Rails 8 Course - Beginner to Intermediate!
Preview
    
      
  This video covers how to create a new page in a Rails application, set up routing, configure controllers and views, and establish navigation links between pages.
Resources
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
                      [MUSIC]
                      0:00
                    
                    
                      Now, we can take this one step further,
and
                      0:04
                    
                    
                      I want to give you a better understanding
by creating a new page on the application.
                      0:06
                    
                    
                      The application is gonna
have a page called homepage.
                      0:09
                    
                    
                      So let's go ahead and do that.
                      0:11
                    
                    
                      We're gonna go to the terminal, right?
                      0:13
                    
                    
                      So go to the terminal, hit clear.
                      0:15
                    
                    
                      And inside of here, what we're gonna do
is we're gonna say, rails g controller,
                      0:16
                    
                    
                      we will call it pages, and we'll just
have one page called home for now.
                      0:21
                    
                    
                      You could also add like an about page,
contact page, pricing page, whatever.
                      0:25
                    
                    
                      So I'm just doing this for the purpose of
showing you, hit Enter on that command,
                      0:29
                    
                    
                      and that's going to create a new
controller, a new folder, and a root.
                      0:33
                    
                    
                      So look, if we go to post controller,
I mean, pages_controller,
                      0:36
                    
                    
                      this is a new controller.
                      0:39
                    
                    
                      And then as you can see in the views,
it created a pages folder.
                      0:40
                    
                    
                      And as you can see in the routes,
it created a get ''pages/home''.
                      0:43
                    
                    
                      So this is the root that we need.
                      0:47
                    
                    
                      Now that we have our new page in the code,
                      0:49
                    
                    
                      we want to actually go
to it in the browser.
                      0:51
                    
                    
                      So how do we do that?
                      0:53
                    
                    
                      Well, let's first of all run rails s.
                      0:54
                    
                    
                      Because if we didn't run rails s, we'd get
this error, I'm not sure if you noticed.
                      0:57
                    
                    
                      If we didn't run rails s,
we wouldn't be able to connect.
                      1:01
                    
                    
                      So we need to run rails s.
                      1:03
                    
                    
                      So run rails s.
                      1:05
                    
                    
                      And then,
how do we actually go to this page?
                      1:06
                    
                    
                      Well, if we go to pages/home,
it should actually work.
                      1:08
                    
                    
                      And I did pages.home,
pages/home does actually work.
                      1:12
                    
                    
                      That's pretty cool.
                      1:16
                    
                    
                      We now have a new page on our
application and we can go there.
                      1:16
                    
                    
                      But what if we want to change the URL
of this page to be the homepage, right?
                      1:19
                    
                    
                      Then what we could do is
we could go to routes.rb.
                      1:23
                    
                    
                      So if I just show you where it is, again,
routes is in config and routes.rb.
                      1:26
                    
                    
                      Or you can do Ctrl+P, search for
it, and it's in routes.rb.
                      1:31
                    
                    
                      Then inside of here all we
have to do is say root, and
                      1:34
                    
                    
                      we can change this to pages#home.
                      1:38
                    
                    
                      Now, the reason that that
works is because it goes and
                      1:40
                    
                    
                      finds the controller that has the name of
pages and then it goes to the home method.
                      1:43
                    
                    
                      So, I'll say that once again, it finds
the controller that has the name pages and
                      1:48
                    
                    
                      it goes to the whole method.
                      1:52
                    
                    
                      This is extremely important.
                      1:53
                    
                    
                      So anytime you see a hashtag,
                      1:54
                    
                    
                      the word after the hashtag is
the name of the method it's going to.
                      1:56
                    
                    
                      If we look at this, get 'pages/home',
that's something different.
                      1:59
                    
                    
                      It's going to the view.
                      2:02
                    
                    
                      Go to app>controllers>pages_controller.
                      2:03
                    
                    
                      As you can see,
this is called the pages_controller, and
                      2:05
                    
                    
                      it has a method called home.
                      2:08
                    
                    
                      So that's why this all works.
                      2:09
                    
                    
                      It's all interconnected.
                      2:11
                    
                    
                      And so, now if we save that routes.rb
file, I just go back to it.
                      2:12
                    
                    
                      I think I forgot to save.
                      2:16
                    
                    
                      Then root of the application
should actually be this page.
                      2:17
                    
                    
                      As you can see, it is.
                      2:20
                    
                    
                      So now if we want to change this page,
all we have to do is just go to
                      2:22
                    
                    
                      views>pages>home, and
then we just change this, all this HTML.
                      2:26
                    
                    
                      We can say, this is the home of the app.
                      2:31
                    
                    
                      As you can see,
this is the home of the app.
                      2:38
                    
                    
                      We can just add a full stop and,
yeah, so that's how this works.
                      2:40
                    
                    
                      Now, one thing that I do want
to show you is this command.
                      2:44
                    
                    
                      So it's called rails routes, and
                      2:46
                    
                    
                      this gives us all of
the routes in the application.
                      2:48
                    
                    
                      And there's so much stuff here
that you have to look at, but
                      2:50
                    
                    
                      the real important parts
are the following.
                      2:53
                    
                    
                      So look, prefix and verb, and then
the URI pattern, and over here as well.
                      2:55
                    
                    
                      So the controller action.
                      2:59
                    
                    
                      So as you can see it says,
new_post, right?
                      3:01
                    
                    
                      So the new_post, and it says, post#new.
                      3:03
                    
                    
                      So new_post goes to post#new.
                      3:06
                    
                    
                      So if we want to create
a link to post#new,
                      3:08
                    
                    
                      we want to go to create a new post,
then we would say new_post_path.
                      3:11
                    
                    
                      And if we go to VS Code and
then we go to the posts>index.
                      3:15
                    
                    
                      As you can see, it says new_post_path.
                      3:19
                    
                    
                      So that's how that kinda works.
                      3:21
                    
                    
                      So if we take another example,
if we go to the show page, and then we go,
                      3:23
                    
                    
                      look at this link, Edit this post,
go to edit_post_path.
                      3:27
                    
                    
                      And then we also take
in the variable @post.
                      3:31
                    
                    
                      And if we look at the roots,
edit_post, and then goes to post#edit.
                      3:33
                    
                    
                      So that's perfect.
                      3:38
                    
                    
                      And then after all of these,
after all of these prefixes,
                      3:38
                    
                    
                      you add an underscore path, and
that's how you get the root.
                      3:41
                    
                    
                      So it's kind of like
a mathematical equation.
                      3:44
                    
                    
                      So if we want to go to the posts>index
page from the homepage,
                      3:46
                    
                    
                      how would we do that?
                      3:49
                    
                    
                      Well, we'd have to create a link.
                      3:50
                    
                    
                      But what would be in the link?
                      3:51
                    
                    
                      Given our previous example,
can you work it out?
                      3:52
                    
                    
                      Well, the answer is,
it's gonna be posts_path.
                      3:55
                    
                    
                      So I'll show you now.
                      3:57
                    
                    
                      So if we go to VS Code and
then we go to pages>home,
                      3:58
                    
                    
                      inside of here we're
gonna create a new link.
                      4:00
                    
                    
                      We're gonna say link_to, we'll say Posts.
                      4:02
                    
                    
                      We'll just say posts_path.
                      4:06
                    
                    
                      That's it.
                      4:08
                    
                    
                      So that should create
a new link to the posts.
                      4:09
                    
                    
                      So as you can see, if I refresh and
I have to start the server,
                      4:12
                    
                    
                      forgot to start the server.
                      4:15
                    
                    
                      So I'll do, clear, and
run it again on rails s.
                      4:16
                    
                    
                      Go back to this, as you can see,
now I can go to Posts.
                      4:21
                    
                    
                      And now I'm on the post page.
                      4:24
                    
                    
                      Let's say we want to go back to
the homepage, how would we do that?
                      4:26
                    
                    
                      Well, we go to the posts,
so we go to the post>index.
                      4:28
                    
                    
                      And then inside of here,
what we do is create a new link.
                      4:31
                    
                    
                      Where do we want the link?
                      4:33
                    
                    
                      Well, you can just have
the link below this new post.
                      4:34
                    
                    
                      So I'm gonna copy and paste this new post.
                      4:38
                    
                    
                      And then inside of here we're gonna
change this to, we can change it to,
                      4:40
                    
                    
                      Back to home, for
now cuz we only have two pages.
                      4:45
                    
                    
                      And then we can say, root_path.
                      4:48
                    
                    
                      So here we can say root_path because
we set it as the root in config.routes.
                      4:53
                    
                    
                      And now if we refresh,
we should have the root to homepage.
                      4:59
                    
                    
                      We can go there.
                      5:03
                    
                    
                      And now we have some functionality in
our application using Rails, right?
                      5:04
                    
                    
                      Might just add a BR tag.
                      5:07
                    
                    
                      So that's all I wanted to show you,
                      5:09
                    
                    
                      now you have a brief overview of
how things actually work in Rails.
                      5:10
                    
                    
                      You know that these files are connected
to the controllers, you know a little bit
                      5:13
                    
                    
                      more about routes and how to create links,
and it's extremely important.
                      5:17
                    
              
        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