Locked lesson.
About this lesson
Creating a framework to allow people to add images to your app.
Exercise files
Download this lesson’s related exercise files.
Pins Scaffold.docx58.6 KB Pins Scaffold - Solution.docx
58.8 KB
Quick reference
Pins Scaffold
In this video we'll create a Scaffold to handle all our pin stuff.
When to use
A scaffold is used to automatically generate all sorts of things, from views, to routes, and controllers, to models.
Instructions
A scaffold will create the framework to build out our pins functionality. It will automatically generate our views, controllers, routes, and models for all our pin stuff.
To create a scaffold, run this command:
rails g scaffold pins description:string
The description:string bit at the end adds a description field to our new pins model.
Once you run your scaffold generator, be sure to rake db:migrate and heroku run rake db:migrate to push your new migration.
Hints & tips
- Scaffolds are useful to create frameworks
- Scaffolds will create views, controllers, models, and routes all at once
- Be sure to rake db:migrate after you run your scaffold generator
- 00:05 Okay, now it's time to start building out the main part of our app to add these
- 00:09 different pins.
- 00:10 And to do that, we're gonna create something called the scaffolding.
- 00:12 And a scaffolding is very much like the very beginning of the course when we
- 00:16 created our first web page.
- 00:17 We used the Rails generator to generate as a controller.
- 00:20 Well now we're gonna generate a scaffold.
- 00:22 And a scaffold is, like it sounds, it's a big scaffolding.
- 00:25 It goes around everything.
- 00:26 It creates a controller.
- 00:28 It creates routes.
- 00:28 It creates views.
- 00:29 It creates database models, which we're gonna be using, especially.
- 00:32 It creates everything for us.
- 00:34 So if you haven't saved your code in a while, go ahead and save your code.
- 00:38 And after you've done that, let's go ahead and issue the command rails g,
- 00:43 for generator, and then scaffold.
- 00:45 And then let's name this pins.
- 00:47 And we can name it anything we want, but we're gonna be creating pins.
- 00:52 So I'm just gonna name the database and the scaffolding pins.
- 00:56 Now we also wanna add something called a description,
- 01:00 and we're gonna call that a string.
- 01:02 And what that is is, basically this is a description, this is the description.
- 01:06 Every pin will have a description that people can type in.
- 01:10 Now, every pin will also have an image, but we're not gonna do that right now.
- 01:13 We're gonna work on that later.
- 01:15 Right now we're just gonna build the basic structure of this thing, the scaffolding.
- 01:19 So rails g scaffold pins description:string, so hit Enter.
- 01:24 And so you can see it's created a whole bunch of stuff for us.
- 01:28 And the main thing to notice is that it's created, let's close some of these out.
- 01:32 It's created a new database migration.
- 01:35 Remember we created one earlier with devise, not it's created a new one.
- 01:39 And this one all it has in it is this description thing, and also a time stamp.
- 01:43 Every time you create a migration, it also creates a time stamp automatically.
- 01:47 You don't really need to know what that is.
- 01:48 But it can be useful for some things.
- 01:50 Anyway, this migration has just created this simple string and a description.
- 01:55 So whenever we create a migration,
- 01:57 whenever we create any sort of database stuff, we need to rake db migrate command.
- 02:01 So rake db:migrate.
- 02:05 And this is pushing our migration into the database.
- 02:08 Now we also need to do the same thing for our Heroku accounts.
- 02:12 So we type in, heroku run and then rake db:migrate,
- 02:16 like we did earlier, and that will do that.
- 02:20 While it's working on that, we can come down to our schema file.
- 02:23 And we looked at this earlier, and
- 02:25 this is all the devise database stuff that we created earlier.
- 02:28 And if you look, we've created a new table in our database called pins.
- 02:32 It has a description with a string and then our time stamp stuff.
- 02:36 So we've added a whole new thing to our database,
- 02:39 and we did it just by typing that one single command.
- 02:42 And that's really cool.
- 02:43 So what else happened?
- 02:45 If we look in our app> views folder.
- 02:49 We have our devise folder in our home, in a layout.
- 02:52 But now we have this pins folder as well, and it's got all kinds of stuffs in there.
- 02:56 If we run our rake routes command that we've done in the past to see what all of
- 03:01 our routes are, we see all of these are the ones that we created for devise.
- 03:06 And here's our two original ones.
- 03:08 If you look up here at the top, there's a whole bunch of new pins routes.
- 03:14 So let's look at this, we've got pins, we've got new pins, we've got edit pin.
- 03:19 So let's just come up to our app, and type pins and see what we got.
- 03:24 So we've got this Listing Pins page.
- 03:26 If we create a New Pin, this is our first pin, hit Enter.
- 03:30 Pin was successfully created, actually it's doing that twice.
- 03:35 We'll have to change that in a bit.
- 03:37 If we go back, we see this is our first pin.
- 03:40 We can show, edit, or destroy that pin.
- 03:43 If we show it, it just shows it.
- 03:44 And you see there's no image here, we haven't done that yet.
- 03:47 It's just the description, but that's kind of cool.
- 03:50 If we click New, pin two, if we hit Back,
- 03:55 this is our first pin, pin two, new pin, pin three.
- 04:01 Create Pin.
- 04:03 Back and we can destroy these, and it deletes them.
- 04:09 Or we can edit it and add a couple more exclamation points.
- 04:17 So that's really cool.
- 04:17 Basically, we've got now the structure to build out the rest of our app.
- 04:21 And this is all database stuff.
- 04:23 We can add things.
- 04:24 We can edit things.
- 04:25 We can delete things, destroy things.
- 04:27 And in the next couple of videos,
- 04:29 we'll work on making this look a little bit better.
- 04:31 But if you think about it,
- 04:32 that's pretty much the functionality that we need to run the whole thing.
- 04:36 We need to have people be able to log in and log out, and then add pins.
- 04:40 And we've basically, in a very basic form, we've got that done already.
- 04:44 So just think how powerful that is.
- 04:46 One command is all it took that rails generate scaffolding command.
- 04:49 And then rake db:migrate and boom, we've got a lot of stuff here.
- 04:53 So we'll get into it more in the next video.
- 04:55 That's all for this video.
Lesson notes are only available for subscribers.