1
00:00:00,290 --> 00:00:04,220
We want our slash pages path to
show a list of blog pages but
2
00:00:04,220 --> 00:00:07,710
right now our view template is
just showing a static header.
3
00:00:07,710 --> 00:00:10,570
So let's set our controller
up to load some page data so
4
00:00:10,570 --> 00:00:12,240
that we can include it in our template.
5
00:00:13,420 --> 00:00:17,790
In our pages controller, we will limit the
index method to load a list of all pages
6
00:00:17,790 --> 00:00:19,380
and store it in an instance variable.
7
00:00:21,070 --> 00:00:24,700
So assign to an instance variable @pages.
8
00:00:24,700 --> 00:00:27,990
Notice that I named the instance
variable pages plural and
9
00:00:27,990 --> 00:00:32,640
not page singular since it's going to
hold a collection of multiple pages
10
00:00:32,640 --> 00:00:33,960
then will call the page at all.
11
00:00:33,960 --> 00:00:37,680
Class method to load all page
records from the database.
12
00:00:37,680 --> 00:00:39,690
Let's save this and refresh our browser.
13
00:00:41,930 --> 00:00:44,980
We get an error
uninitialized constant page.
14
00:00:46,180 --> 00:00:48,930
Our controller is receiving
the user's request and
15
00:00:48,930 --> 00:00:50,970
attempting to load data from the model.
16
00:00:50,970 --> 00:00:53,270
But we haven't created
our page model class yet.
17
00:00:53,270 --> 00:00:55,050
It doesn't exist.
18
00:00:55,050 --> 00:00:57,950
So we get an error when we
attempt to reference it.
19
00:00:57,950 --> 00:01:01,610
So let's create a model class
now using Rails generator.
20
00:01:01,610 --> 00:01:05,760
Just as with scaffolds and controllers
rails has a generator for models.
21
00:01:05,760 --> 00:01:10,130
The model generator will create a source
file with the model class for us.
22
00:01:10,130 --> 00:01:13,587
At the same time it will create
a migration file that will set up
23
00:01:13,587 --> 00:01:16,326
a database table to hold records for
that model.
24
00:01:16,326 --> 00:01:23,330
Stop the server if it's running and
run the command bin/rails generate model,
25
00:01:23,330 --> 00:01:30,450
because we're generating a model class and
then the name of the model class page.
26
00:01:31,540 --> 00:01:35,820
Then just as with scaffolds we can specify
a series of batch beads we want page
27
00:01:35,820 --> 00:01:39,670
objects to have along with the types of
database columns that should be created to
28
00:01:39,670 --> 00:01:44,990
hold that data we want pages to have title
and body attributes just like with posts.
29
00:01:44,990 --> 00:01:49,650
And then also a slug attribute that we
can use to make human readable URLs.
30
00:01:49,650 --> 00:01:56,570
So we'll add a title attribute and give
it a type of string with a title:string.
31
00:01:56,570 --> 00:01:58,650
Then will hit space and
had a body attribute.
32
00:01:59,990 --> 00:02:02,810
Page bodies are of course
much longer than titles so
33
00:02:02,810 --> 00:02:05,900
we'll give it a type of
text instead of string.
34
00:02:05,900 --> 00:02:08,940
Last will add a slug attribute
with a type of string again.
35
00:02:10,250 --> 00:02:14,225
Hit return to run the command and
it will create files with a model and
36
00:02:14,225 --> 00:02:15,428
a migration for us.
37
00:02:15,428 --> 00:02:19,976
The model classes in app models page.rb.
38
00:02:19,976 --> 00:02:23,380
As with the post model, it's not much
to look at because all its methods
39
00:02:23,380 --> 00:02:25,380
are inherited from application record.
40
00:02:26,590 --> 00:02:30,660
The migration is in a file
within the db migrate directory.
41
00:02:32,580 --> 00:02:35,450
It has commands to create a Pages table
42
00:02:35,450 --> 00:02:39,990
with all of the fields we specified on
the command line by default it also has
43
00:02:39,990 --> 00:02:43,980
a call to the timestamps method
which was set up created that and
44
00:02:43,980 --> 00:02:48,510
updated that columns the track when
a record was created or modified.
45
00:02:48,510 --> 00:02:49,810
It's not mentioned here but
46
00:02:49,810 --> 00:02:53,490
rails will automatically create
the all important ID column.
47
00:02:53,490 --> 00:02:57,220
That's used for
looking individual page records up later.
48
00:02:57,220 --> 00:03:01,120
We've created the migration, but
the database is an updated yet.
49
00:03:01,120 --> 00:03:05,130
Remember we got a pending migration error
last time we created the migration without
50
00:03:05,130 --> 00:03:09,880
updating the database to avoid seeing
that next time we launch the server.
51
00:03:09,880 --> 00:03:15,510
We need to run been/rails db: migrate,
52
00:03:15,510 --> 00:03:18,420
which will run the migration and
create the database table.
53
00:03:19,520 --> 00:03:23,190
Now let's run the server
again with bins/rails s.
54
00:03:23,190 --> 00:03:26,450
And then reload our browser.
55
00:03:26,450 --> 00:03:29,000
It will load successfully this time but
56
00:03:29,000 --> 00:03:32,010
no pages will be displayed
will fix that next.