Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Preview
Start a free Courses trial
to watch this video
In this workshop, we'll learn how to create static pages in a Rails application.
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
[MUSIC]
0:00
Hi, I'm Jason,
your Ruby teacher here at Tree House.
0:05
In this workshop, we're going to
be going over how to create static
0:09
pages in a Ruby On Rails application.
0:12
And for the purposes of this workshop,
we're going to consider a static page,
0:15
something that isn't necessarily backed by
the database, or a specific controller.
0:19
We would use static pages for things like
the terms of service, privacy policy, or
0:24
anything that we don't really
need to query in the database.
0:29
Let's go ahead and get started.
0:32
All right, so before we can go through and
create our static pages in a Rails
0:36
application, let's go ahead and create a
fresh Rails application to try this with.
0:41
So I'm gonna say, rails new static_pages.
0:47
Now let's go ahead and
go into this application and
0:53
just type bundle to get
everything working.
0:57
Okay.
0:59
Now clear my screen there.
1:01
And let's go ahead and
start the rails server.
1:04
And just go ahead and
make sure everything is working.
1:06
I go to localhost on port 3,000.
1:10
Alright we are riding Ruby on Rails.
1:15
Great work.
1:18
And now here is the Atom Text editor
with the application we just generated.
1:20
So now, let's go ahead and
go back to our console here.
1:27
And I am going to stop the rails
server for just a moment.
1:31
And I'm gonna say, rails generate
controller pages, and since we don't
1:37
want any CSS or JavaScript, I'm gonna
tell it to skip generating the assets.
1:42
So, our pages controller, if we head on
back to the application here in Atom,
1:49
we'll open up our controllers,
and here is our pages controller.
1:54
So, what we wanna do is,
have a method in our pages controller or
2:00
an action, to display our static pages.
2:05
And for right now,
we're just going to create a method
2:10
called show that will render the page.
2:14
Now the pages controller
is going to display
2:18
all of these static pages when
we go in the application.
2:22
So we have this views directory and
pages, which is empty.
2:24
Now, let's go ahead and
open up our routes file.
2:29
And that'll be in the config directory.
2:32
Now, I'm going to go ahead and just
take out all of the generated comments.
2:36
And what we're going to do is
say whenever we go to /pages,
2:42
/whatever, we want to send
that to our pages controller.
2:46
So, when we get pages/page,
this is gonna go
2:52
to the pages controller we just generated,
and the show method or action.
2:58
So, once we set up this route,
that goes to the pages controller and
3:05
the show action, we're telling it that
we want this page parameter sent in.
3:09
Which means we'll have access to it inside
of the show action, as the page parameter.
3:15
So once we know that we have that,
3:23
we can use some string interpolation to
tell Rails to render that as a template.
3:25
We can say render template and
then we'll just say,
3:31
go inside of the pages directory and
3:36
grab whatever it is that comes
in as the page parameter.
3:40
So if we did something like pages/about.
3:47
And we'll call this html.erb since
that's gonna be our template.
3:54
We'll just say,
3:57
About this Application.
4:01
And let's go ahead and
generate some text in here.
4:06
I'm going to use
the cupcake ipsum webpage.
4:09
To generate some text.
4:13
We'll say we want three paragraphs.
4:16
All right!
And this is going to look delicious.
4:18
Let's go back to our About page.
4:22
Paste that in here.
4:25
And I'll add some paragraph tags as well.
4:26
Okay so we've got this about page and
4:34
if we go to /pages/about,
4:40
this should render the template
that we just created.
4:44
So let's go ahead and give that a try.
4:52
/pages/about.
4:54
Oh!
Not available because
4:56
we need to start our rail server.
4:59
Okay, so now if I reload,
we now have this about page.
5:02
Which looks pretty good, and
actually lets go ahead and
5:08
just make sure that this looks great.
5:12
We'll say, H1,
this is static pages example application.
5:17
And everything goes inside this main div.
5:22
And I'll change this to an h2 tag.
5:33
So, now if I reload.
5:36
Okay.
We know we are using the regular
5:42
application layout and
our static page is rendering correctly.
5:45
But if we go to thing that doesn't work,
it's going to say the template is missing.
5:51
Now we're gonna fix that in just a second.
5:57
First let's go ahead and
create a home page.
6:01
So if we look at our application
right now, we have the regular
6:05
default rails application,
welcome to rails page.
6:11
But, if we wanted to generate a static
homepage- let's go ahead and do that.
6:16
We're gonna save pages/home.html.erb.
6:19
And let's just say this is our home page.
6:22
So now we'll make sure that works.
6:33
We'll go to pages/home.
6:35
All right, so
we have our static homepage that works but
6:37
what happens if we go to localhost 3000?
6:40
That doesn't work just yet.
6:42
Now we can use a directive
in our routes file
6:46
to tell Rails that the root
should be the PagesController,
6:51
And the show action, and
6:57
we can send in the parameter
page to be the home page.
7:02
So the route method inside of the route,
7:07
tells rails what to use as
the route of the application.
7:12
So if we reload here,
7:15
we can see that we now have
a static homepage in our Rails app.
7:17
So that looks really good.
7:23
But let's go ahead and go back and
handle that 404 error.
7:24
So if we go to a page that's not found.
7:28
All right,
we need to render this template.
7:32
So, how are we gonna do that?
7:36
Well, we're gonna handle that
inside of our PagesController.
7:37
And, first what we're gonna do is
create a private method inside of our
7:42
PagesController called valid_page.
7:46
And what this method is gonna do
7:51
is check to make sure that
this template exists on disk.
7:53
And we're gonna doing that by just saying,
hey, does this file exist?
7:58
Now once we have this file exist method
8:03
we need to pass in the parameter
of the page that's coming in,
8:07
so we'll say, does this page
exist that we're looking for?
8:11
Params page, that'll be interpolated.
8:16
Then say .html.erb.
8:20
Now, that's not going to do
exactly what we want it to
8:23
because we need to tell
Rails exactly where it is,
8:27
it's not gonna know just based on this
small string where it is on disc.
8:30
So we could say rails.root plus, and
then send in the entire path app/views.
8:36
Pages, and
then the page that we're looking for.
8:44
And we are almost complete there.
8:48
We're gonna use the Pathname class
8:50
to create an actual path
name that Rails can look at.
8:55
Just to make sure everything
is completely working here.
9:00
And let's make sure our parentheses close,
as well.
9:02
That looks like we need one more.
9:06
Okay, so we can be reasonably sure
that this valid page method works.
9:10
Saying okay, hey, is this an actual
page that exists on disc?
9:14
Now we can say, If it's a valid page,
go ahead and render that template.
9:19
If not let's go ahead and
render a 404 page.
9:27
Now, Rails comes with one, and
we're just gonna use that for right now.
9:33
And that is in the public directory.
9:37
So we're gonna tell Rails, okay,
render this file 404.html.
9:40
And we'll give it the status of
not found because we wanna be
9:46
a good net citizen and send back a 404
status if the page is not found.
9:51
So let's go ahead and
reload this 404 page.
9:57
All right!
The page you were looking for
10:01
doesn't exist.
10:03
That is exactly what we want.
10:04
And let's just go ahead and
make sure our other page works.
10:08
Okay.
The about page works.
10:10
Home page works.
10:13
And if we go just to the route,
that still works as well.
10:16
Now, that's pretty good, but
let's say we wanted to create
10:20
a marketing home page here, and we had
a bunch of features in our application,
10:24
and we were just gonna display them all,
one by one.
10:28
Then let's go to pages/features.
10:33
And then we get this little routing error,
saying, no route matches,
10:36
get pages features one.
10:39
Well, let's go ahead and create that,
just so that we know it's there.
10:42
New folder, pages, features.
10:45
And this would be our first feature.
10:53
Okay, so now if we reload,
we know this exists, but
11:04
still we're not getting any route for
this.
11:07
And the reason is, we are only getting
an incorrect parameter being sent in.
11:11
Right, if we look back
at our log we're saying,
11:18
we're getting pages/features/one.html.erb,
and that's not getting a route.
11:20
But, we do have a route set up in here.
11:26
So, we can make one very
small change to this and say,
11:32
instead of getting page as a parameter,
we're going to use this Splat operator.
11:36
And what that's gonna do is,
it's gonna break all of this up.
11:43
So if we had a route pages/about,
the page parameter would be "about".
11:48
But, when we add this into a splat
parameter, this will come in as an array.
11:57
So pages features one doesn't come in
as a page parameter, nothing's found.
12:04
However, when we change it to the splat,
we get features one.
12:11
Then I'm gonna just take that out of here
and let's go ahead and just reload and
12:19
see how that looks.
12:23
Oh, Not found.
12:24
Here we go, feature one, and that worked.
12:26
Now if we look back in our log.
12:30
Page parameter is now features/one.
12:32
It is one string here and
the slash is part of it.
12:38
So that will work.
12:44
And let's make sure our
other page still works.
12:45
Yes pages/about works.
12:49
Pages/home works.
12:52
The home page still works.
12:55
And let's just make sure
our 404 page works.
12:57
And that still works as well.
13:02
So, great work everybody.
13:04
That was a quick little tutorial
on how to create static pages
13:05
in a Rails application.
13:08
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