Locked lesson.
About this lesson
What is the Pins Controller and how do we use it?
Exercise files
Download this lesson’s related exercise files.
Understanding The Pins Controller.docx58.7 KB Understanding the Pins Controller - Solution.docx
58.9 KB
Quick reference
Understanding the Pins Controller
In this video we'll learn about the complicated looking pins controller file.
When to use
The pins controller is located at app/controllers/pins_controller.rb and it handles all our pins functions like adding, editing, showing and removing pins.
Instructions
Add these two before actions to the top of your pins_controller.rb file:
before_action :authenticate_user!, except: [:index, :show]
before_action :correct_user, only: [:edit, :update, :destroy]
Hints & tips
- Each method in the controller file corresponds to a C.R.U.D. action and view
- Before actions allow us to run some logic "before" anything else happens
- 00:04 Our pins and our users are now associated at the database level.
- 00:10 Now we need to make sure they're associated at the view level and
- 00:13 the controller level.
- 00:14 So we haven't actually looked at the pins controller yet.
- 00:17 But if you go to apps > controllers > pins_controller,
- 00:20 this file was created when we ran the scaffold command a couple of videos ago.
- 00:24 And earlier we had a home controller and
- 00:26 all it had was these two little methods right here with nothing in them and
- 00:29 there's nothing complicated about that at all.
- 00:32 But now we have this pins controller and there's all kinds of stuff in here.
- 00:35 And so I wanna take just a minute to talk about this and what's going on.
- 00:39 So when we created our scaffolding, it gave us all this stuff, the ability to
- 00:43 create a new pin, to show pins, to edit pins, to destroy pins, and all that stuff.
- 00:48 Well, this is where all of that takes place.
- 00:50 This is where the brains go.
- 00:52 Remember when we talked about MVC, the model view controller?
- 00:56 The controller was sorta the brains behind the scene.
- 00:59 Well, here you're seeing that.
- 01:00 It looks overwhelming, there's a lot of stuff going on here.
- 01:02 But if we kinda look at here and break it down,
- 01:05 you see these are Ruby methods, each def and end, def and end.
- 01:09 And if you look, there's an index, a show, a new, an edit, create, and
- 01:14 an update and a destroy.
- 01:16 And these correspond with our CRUD, create read update and destroy and
- 01:20 also these all these pages that our scaffolding created.
- 01:23 So whenever you create a new pin here and click the submit button,
- 01:28 what's going on is behind the scenes this new method is getting called in
- 01:33 your pin controller, because we're creating a new pin.
- 01:36 And right here, this is all the code it takes for
- 01:40 Rails to add that new pin to our database, right?
- 01:44 This is an instance variable that collects all of
- 01:47 the stuff that you typed into this field.
- 01:50 And then it pops it into our database as a new thing, right?
- 01:55 When we wanna edit, actually there isn't anything, create, that also goes with new.
- 02:00 Pin params, that is the stuff in your form.
- 02:05 They each become a parameter.
- 02:07 So you don't really need to know a whole lot what all this stuff is.
- 02:10 Update whenever we wanna update a pin, it does all this stuff.
- 02:13 Destroy, this is the code to destroy.
- 02:15 But like I said,
- 02:16 the scaffold created all this for us so we don't need to know what all this stuff is.
- 02:20 Except now that we wanna make some changes and associate pins and
- 02:23 users, we have to come in here to the controller and make a few changes.
- 02:26 The first thing we're gonna do is right here at the beginning,
- 02:29 we need to add a couple more before actions.
- 02:31 Now I'm just gonna paste these in and let's take a look.
- 02:34 What these are doing is, before anything on this page happens,
- 02:38 we're gonna do this stuff and this stuff.
- 02:41 So what is this stuff?
- 02:42 Well before anything else, we need to authenticate a user.
- 02:45 So if somebody wants to edit a pin, for instance,
- 02:48 we need to authenticate them before they do that.
- 02:51 And we're gonna do that for all of these methods, except for
- 02:55 the index method, this one, and the show method, this one, right?
- 03:00 So why is that?
- 03:01 Well, if somebody just wants to look at our index page,
- 03:03 we don't care if they're logged in, we don't care if they're authenticated.
- 03:06 If somebody wants to look at a specific pin to show a pin,
- 03:09 we don't care if they're logged, we don't need to authenticate them.
- 03:12 But for everything else they need to be logged in or authenticated.
- 03:15 And this little line will throw up an error if somebody tries to edit a pin and
- 03:19 they're not logged in, it'll throw up an error.
- 03:21 Next, we need to figure out who the correct user is.
- 03:25 And by correct user, we mean a user that owns a pin.
- 03:29 So if I wanna edit a pin, the program needs to know that I'm the correct person
- 03:33 that can edit that pin because I own that pin.
- 03:35 That pin belongs to me.
- 03:37 So we create this correct user, and we need to know the correct user for
- 03:41 anybody that wants to edit, update, or destroy a pin.
- 03:44 They have to be the correct user,
- 03:45 they have to own that pen in order to edit it or update it or destroy it.
- 03:49 So this actually doesn't work quite yet.
- 03:51 We need to create a method down here that shows who is the correct user, and
- 03:56 we'll do that in the next video.
Lesson notes are only available for subscribers.