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