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
Scaffolding in a rails application sets up resources for you to work with in your application. This is a really fancy way of saying that it creates a model, controller, migration, etmplates, and assorted files. Scaffolding is a great start to the app but is meant to be replaced over time. In this workshop, you'll learn in depth how it works.
Hi, I'm Jason,
the Ruby teacher here at Treehouse.
0:00
In this Rails Scaffolding Workshop we're
going to take a look at how scaffolding
0:04
works in a Ruby on Rails application.
0:08
Now, what scaffolding is going to do is
set up a few different things for you.
0:11
Models, routes, views, and a controller.
0:15
Now we're gonna take a thorough
look at how to use a scaffold
0:20
in your Rails application.
0:23
Now scaffolding is just meant to get
you up and running with a resource.
0:25
Generally you'll replace
all of this over time
0:30
as you build out your
application's feature.
0:32
But anyway let's take a look
at how scaffolding works now.
0:35
Okay, so let's take a look at
how Rails scaffolding works.
0:39
Right now I am inside of a Rails
application called example app.
0:44
This is a freshly generated
Rails application.
0:49
There's nothing inside it.
0:53
Now, we use scaffolding to set
up resources in our application.
0:56
So, let's go ahead and set that up now.
1:02
Setting up resources is kind of a fancy
way of saying that it's gonna create
1:05
a model, controller, migration,
templates, and some other files for us.
1:09
So, let's go ahead and pretend that we
are creating a to-do list application.
1:14
So, first let me go ahead and
create the databases.
1:19
There we go,
that should have created all my databases.
1:26
Now, let's go ahead And, say,
we're gonna generate a scaffold for
1:28
a todo_list.
1:34
And, when we are generating a scaffold,
let's go ahead and
1:37
just say, our todo list has a title and
that's a string.
1:40
We're not gonna get too
complicated right now.
1:44
So, we do this, we generate
the scaffold todo list, press enter,
1:47
and now we can see a whole
bunch of things just happened.
1:52
It created a whole bunch of files,
and added routes for us.
1:55
Let's go ahead and see what it did.
2:00
So, it created this migration file called
create todo lists, let's go ahead and
2:03
take a look at that.
2:07
Now I'm using Sublime Text here, you
can use any text editor on your system.
2:12
So here we go, it says it created
a migration called CreateToDoLists,
2:19
and you'll notice it's got a string for
the title attribute.
2:25
And the title attribute was what we had on
2:30
the command line when we
generated the scaffold.
2:34
So it also created a model for todo list.
2:38
Let's go ahead and
look at the todo list model.
2:41
So here's the todo list model,
it is just an empty model.
2:48
Let's go back and see what else happened,
we have a test for
2:50
it as well as a fixture.
2:55
Let's go ahead and load up the fixture and
see what that looks like.
2:58
So we open up the test directory,
fixtures, TodoList.yml And
3:02
this created two different fixtures for
us.
3:06
Fixtures are going to be
inserted into our database and
3:11
it's just some test data for
us to work with.
3:14
It added a resource to our
routes called to do list.
3:19
Let's go ahead and look at that.
3:23
So if we open up our config directory and
open up the routes file,
3:25
we now have this line added,
resources, to-do lists.
3:29
So that sets up our todo list routes for
us.
3:34
Which is going to go to
the todo lists controller,
3:37
which is also the next thing
that was generated for us.
3:41
Let's take a look at that.
3:45
So if we open up this to do
list controller we can see we
3:49
have a few different actions here.
3:52
We've also got this before filter
that sets the to do list for
3:54
these particular controller actions show,
edit, update and destroy.
3:59
Let's scroll down and
see what that looks like.
4:04
Well, it looks we have
this set TodoList method.
4:06
This sets up an instance variable, which
finds a to do list by the ID parameter,
4:09
and also where set up
the TodoList params itself.
4:15
This is going to be part
of strong parameters.
4:21
And, it says, inside of the controller,
4:24
that we need this TodoList parameter for
the parameters to be sent in.
4:26
And, we are permitting
the title attribute.
4:32
So, set_todo_list is set.
4:37
Then, we can call it
in the destroy method.
4:38
Update method.
4:41
Create method sets it up itself
based on the TodoList params.
4:43
Edit method, all set.
4:49
To do list just creates a todo list and
4:51
show method also gets it from
the set todoList method.
4:54
Now if we were building up a real TodoList
controller, this would be a good start.
4:59
This wouldn't be everything
that we needed because
5:03
odds are we're gonna be replacing parts
of this throughout the application.
5:06
We might decide that we need more
attributes inside of the todo list.
5:10
Maybe we are going to respond
to more than just HTML and JSON.
5:14
Or, maybe we need to manipulate
the todo list in some way,
5:20
perhaps to add the ability to add files,
not really sure.
5:23
The scaffolding is just meant to be
a starting point for this application.
5:27
So let's see what else it generated.
5:33
Looks like we have some views here for
index, edit, show, new, and
5:35
the form template Let's go ahead and
see what those look like.
5:39
So here is the index file.
5:44
We are listing the todo lists, and
it goes through, and this is interesting.
5:47
It actually displays the title for us and
5:52
then links to all of
these different actions.
5:55
Go back here.
It's the same thing for the edit,
5:59
show, and new pages.
6:01
Let's go ahead and
take a look at new though.
6:03
We just render the form same
thing on the edit page.
6:05
Here is the form file.
6:09
The form template partial just specifies
it's a form for the todo list.
6:12
Shows you any errors that happen
if we tried saving the to do list.
6:17
And then it creates a field for
the title and a submit button.
6:22
And this is all done for us.
6:27
Finally we get a controller test and
a todo list helper.
6:30
The controller test,
let's take a look at that.
6:35
It's down here.
6:38
Now this is going to go through.
6:41
It uses the fixtures that were set up and
just asserts that all these
6:42
things are working as expected which is
that things are going to require a title.
6:48
It's given a title for us and
6:55
everything is kinda just happening as
we expect it to inside of a controller.
6:56
Helper is going to be empty because we
7:02
don't know what we're going to
need inside of the to do list.
7:05
So helpers are going to be used for
adding view logic inside of method.
7:09
So if we wanted to say something
like display to do list header.
7:15
Maybe that has the title
of the to do list and
7:19
its due date for
some of the items on there.
7:23
We don't know.
7:26
That would be logic that we
encapsulate inside of this module, but
7:27
there's nothing to do yet.
7:31
But rail sets it up for us.
7:33
And finally we have
the index.json.jbuilder and
7:35
the show.json.jbuilder.
7:40
If we were creating an API we would
be using these different files.
7:42
Sorry the, here's the json builder.
7:48
We would be using the json builder files
to say okay, here's our todo list,
7:50
give me the ID, title, created_at and
updated_at attributes.
7:56
These created_at and updated_at attributes
are set inside of the migration,
8:00
that's what this timestamps method does,
creates created_at and updated_at.
8:05
And then when you create or
8:10
update a record, those are automatically
updated for you in Rails.
8:13
And then finally we have a CoffeeScript
file generated as a JavaScript file.
8:19
And a todo_lists style
sheet created as well
8:25
in these could be used
inside of our application.
8:30
This is where we would put our JavaScript
logic if we had something just for
8:31
the todo list controller.
8:36
And, the same thing.
8:39
If we had todo list stylesheets that
just needed to be applied to todo lists.
8:40
But, let's go ahead and
look at the assets here.
8:46
By default, the application in
JavaScript includes everything.
8:51
So that is going to include
the todo list CoffeeScript file.
8:56
You can change this if you wanted to.
8:59
Take it out of the required_tree
if you wanted more
9:01
fine grain control over when
these things are loaded.
9:05
If not, it's going to apply
to the whole application and
9:07
not just your todo list controller.
9:10
So, that's up to you.
9:13
Same thing occurs in the style sheet.
9:13
And this scaffold CSS file is something
that's generated by Rails automatically.
9:17
It just gives you some basic styling for
Rails application.
9:22
It's created when you generate
a scaffold if it doesn't exist already.
9:25
So we ran the scaffold.
9:30
I'm gonna clear my screen here and let's
go ahead and migrate the database, okay.
9:32
And let's see what this actually did for
9:40
us now that we saw all
the files that were generated.
9:41
So if we open up the Rails
application right now we can see
9:48
this is the default Ruby on Rails page.
9:51
If we wanted to get to
what we just created.
9:55
We would go /todo_lists and
9:58
this would be the scaffold
that was generated.
10:02
So I could create a todo list and
10:06
this will tell me the todo
list was successfully created.
10:11
I could view it,
could edit it if I wanted to.
10:14
And I could also destroy it.
10:18
So that's a Rails scaffold.
10:23
I'm gonna go ahead and
stop the server here and clear the screen.
10:25
Now let's say we wanted to generate
another scaffold for todo items.
10:30
We could do the same thing,
rails generate scaffold todo_item,
10:38
and we will say that has a title,
which is a string,
10:44
and a due_at column, which is a date time.
10:50
And furthermore, let's go ahead and
10:55
say that these to do items
belong to the to do list.
10:57
We could say that by saying that the to
do item references the to do list.
11:01
We could say, to do list.
11:06
And we give it this key, references.
11:08
Now here's something neat that
you can do with scaffolding.
11:12
I'm gonna use a dash and the letter P and
11:14
that's going to pretend to
create the scaffold for me.
11:17
So this didn't do anything,
but it said it was going to.
11:20
It was going to create a migration and
it's going to create a model.
11:24
Basically everything that
was created last time.
11:29
But if we look inside of
the application here,
11:32
we can close these,
we'll notice that we didn't
11:37
get the controller,
we didn't get the models either.
11:42
So adding -p as a flag will pretend
to generate this scaffold for us.
11:47
Now, I'm gonna go ahead and
just generate that.
11:53
Okay.
11:57
Now, let's take a look at this references.
11:59
If you want to set up
a relationship of some sort.
12:01
Here, we're using references for
todo_list.
12:06
And we're indexing that database column
and also giving it a foreign key.
12:10
So if we where to migrate
the database right now.
12:16
Okay, we create the todo_items table, and
12:24
if we look back at the todo item model, we
can say that it belongs to a to do list.
12:28
What you'll notice is that
the scaffold does not tell the to do
12:36
list that it has many to do items.
12:41
So if we wanted to do any
kind of different relation
12:46
we would have to add that
in this migration as well.
12:49
Now in addition to being able
to pretend what we wanted.
12:53
Let's go ahead and take a look
at the scaffold generator help.
12:57
So if we add two dashes and
the world help.
13:07
We can see we have a bunch of different
options here when we are generating
13:11
the scaffold.
13:13
And this will give us more control
13:15
over the different scaffolds
that are generated.
13:18
So let's say we were scaffolding
the to-do items like we just did,
13:21
and we wanted to index the name.
13:25
We could say, indexed field, give it a
type, and also another colon at the index.
13:28
Now as far as options go,
13:35
here's everything we can pass
to a scaffold generator.
13:36
Are we generating style sheets?
13:41
This defaults to true.
13:43
Are we generating style sheets with SCSS?
13:45
The default is SCSS.
13:49
Now we could say that We want to
skip asset generation entirely which
13:51
includes JavaScript by saying no assets or
skip assets.
13:56
Are we scaffolding a controller?
14:02
The default is scaffold controller.
14:05
We also get active record options.
14:08
Do we want to generate migrations,
timestamps,
14:10
parents which will also add indexes and
references for belongs_to columns.
14:13
You'll notice that defaults to true
14:18
which is what we said on the command
line before with references.
14:21
As we scroll through we can see we
have all of these built in options.
14:26
So, in addition to pretend, with -p,
which is what we added before,
14:33
we can also call that with two hyphens and
pretend.
14:38
If we wanted to make sure that
everything was completely overwritten,
14:42
we could use the force, or -f option.
14:46
We could suppress output by doing --quiet,
or -q.
14:50
Then we could skip files that already
exist by typing --skip, or -s.
14:55
Now we can change all of these things
from the defaults as we're generating
15:05
the scaffolds.
15:08
It tells you what the defaults are in
the regular general command with help.
15:10
And those are the basics of working
with Scaffolding in a Rails application.
15:18
You need to sign up for Treehouse in order to download course files.
Sign up