Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Creating a Model3:32 with Jay McGavren
We want our /pages path to show a list of blog pages. But right now, our view template is just showing a static header. So let's set our controller up to load some Page data so that we can include it in our template.
To run the model generator from your terminal:
bin/rails generate model ModelName attr1:type attr2:type
ModelName is the name of the model class to create (like
attr2, etc. are the names of attributes (like
body), and each
type entry is the type of the attribute (like
We want our slash pages path to show a list of blog pages but
right now our view template is just showing a static header.
So let's set our controller up to load some page data so
that we can include it in our template.
In our pages controller, we will limit the index method to load a list of all pages
and store it in an instance variable.
So assign to an instance variable @pages.
Notice that I named the instance variable pages plural and
not page singular since it's going to hold a collection of multiple pages
then will call the page at all.
Class method to load all page records from the database.
Let's save this and refresh our browser.
We get an error uninitialized constant page.
Our controller is receiving the user's request and
attempting to load data from the model.
But we haven't created our page model class yet.
It doesn't exist.
So we get an error when we attempt to reference it.
So let's create a model class now using Rails generator.
Just as with scaffolds and controllers rails has a generator for models.
The model generator will create a source file with the model class for us.
At the same time it will create a migration file that will set up
a database table to hold records for that model.
Stop the server if it's running and run the command bin/rails generate model,
because we're generating a model class and then the name of the model class page.
Then just as with scaffolds we can specify a series of batch beads we want page
objects to have along with the types of database columns that should be created to
hold that data we want pages to have title and body attributes just like with posts.
And then also a slug attribute that we can use to make human readable URLs.
So we'll add a title attribute and give it a type of string with a title:string.
Then will hit space and had a body attribute.
Page bodies are of course much longer than titles so
we'll give it a type of text instead of string.
Last will add a slug attribute with a type of string again.
Hit return to run the command and it will create files with a model and
a migration for us.
The model classes in app models page.rb.
As with the post model, it's not much to look at because all its methods
are inherited from application record.
The migration is in a file within the db migrate directory.
It has commands to create a Pages table
with all of the fields we specified on the command line by default it also has
a call to the timestamps method which was set up created that and
updated that columns the track when a record was created or modified.
It's not mentioned here but
rails will automatically create the all important ID column.
That's used for looking individual page records up later.
We've created the migration, but the database is an updated yet.
Remember we got a pending migration error last time we created the migration without
updating the database to avoid seeing that next time we launch the server.
We need to run been/rails db: migrate,
which will run the migration and create the database table.
Now let's run the server again with bins/rails s.
And then reload our browser.
It will load successfully this time but
no pages will be displayed will fix that next.
You need to sign up for Treehouse in order to download course files.Sign up