1
00:00:00,270 --> 00:00:01,060
Welcome back.

2
00:00:01,260 --> 00:00:07,020
So we have another video about comments finishing up crud we're focusing on the D which is delete.

3
00:00:07,350 --> 00:00:13,530
So to delete a comment we need to add in our route and we also need to add in the button which is actually

4
00:00:13,530 --> 00:00:17,930
a form that will submit a request to that destroy route.

5
00:00:18,150 --> 00:00:22,140
And this is pretty simple compared to edit and update only one route.

6
00:00:22,140 --> 00:00:23,980
You don't need a crazy complex form.

7
00:00:23,990 --> 00:00:28,860
You don't have to worry about a lot of the complications that we did with adding an update for campground

8
00:00:28,950 --> 00:00:31,550
and in particular comments.

9
00:00:31,590 --> 00:00:33,200
So the route that we need.

10
00:00:33,420 --> 00:00:36,830
Let's take a look at how we did it for campground to start.

11
00:00:37,260 --> 00:00:41,850
We scroll down slash ID and that's it.

12
00:00:41,850 --> 00:00:48,900
But remember our route is actually slash campground slash ID as a delete request so we can add that

13
00:00:48,900 --> 00:01:01,530
in campground destroy route is slash campgrounds slash ID as a delete request and our comments destroy

14
00:01:01,540 --> 00:01:04,300
route is nested so it's a little bit longer.

15
00:01:04,380 --> 00:01:13,500
It's slash campgrounds slash ID slash comments slash comments ID as a delete request.

16
00:01:13,500 --> 00:01:15,320
So we're falling a restful route here.

17
00:01:15,510 --> 00:01:17,770
Let's now go and implement this route.

18
00:01:18,360 --> 00:01:20,530
We'll begin by going to our comment genius.

19
00:01:20,880 --> 00:01:30,150
The routes file and we'll add in a comment that says comments destroy route.

20
00:01:31,290 --> 00:01:41,370
And then we need our router dot delete and the route is just comment ID and that's it because that other

21
00:01:41,370 --> 00:01:46,680
part has been prepended to it when we required it in the US and I've showed that a lot in last few videos

22
00:01:46,700 --> 00:01:46,840
.

23
00:01:46,980 --> 00:01:53,400
So I won't do that again but it's in the US where we require this file and then we need our request

24
00:01:53,400 --> 00:01:56,570
in response standard call back here.

25
00:01:57,120 --> 00:02:06,060
And what we'll do here eventually is find my ID and remove.

26
00:02:06,570 --> 00:02:08,840
But for now we're just going to rest.

27
00:02:08,880 --> 00:02:17,430
And this is the destroy comments route and will save.

28
00:02:17,430 --> 00:02:19,640
Now let's try and hit this route.

29
00:02:19,920 --> 00:02:21,960
So we'll go to our show form.

30
00:02:22,210 --> 00:02:25,500
Let me start the server first.

31
00:02:25,500 --> 00:02:28,320
What we want is a button right here.

32
00:02:28,410 --> 00:02:29,120
Just look at it.

33
00:02:29,130 --> 00:02:34,050
But let's make it red and then click on it and it sends a delete request to that route.

34
00:02:34,200 --> 00:02:39,630
But remember it's not actually a delete request it's a post request because we can't make a delete request

35
00:02:39,630 --> 00:02:43,050
from a form yet it's allegedly coming in the future.

36
00:02:43,050 --> 00:02:45,870
That's the whole method override thing that's why we're using it.

37
00:02:45,870 --> 00:02:50,320
We sent a post request with underscore method equal to to be.

38
00:02:51,030 --> 00:02:53,030
So let's do that now in our show.

39
00:02:53,030 --> 00:03:04,020
So it right below this button here this anchor tag will add in a form and it can't be just an anchor

40
00:03:04,020 --> 00:03:06,180
tag because that only makes you get requests.

41
00:03:06,180 --> 00:03:08,010
We needed a post.

42
00:03:08,040 --> 00:03:11,340
So action it's going to be long again.

43
00:03:11,350 --> 00:03:20,310
We can actually copy what we have above campground slash campground got ID slash comments slash comment

44
00:03:20,310 --> 00:03:23,630
ID so we can copy that whole thing in here.

45
00:03:24,510 --> 00:03:26,210
And we don't need slash edit.

46
00:03:26,460 --> 00:03:34,590
We don't need to slash anything after the ID except we do need method underscore method equals delete

47
00:03:35,460 --> 00:03:41,250
and then we also need method equals post.

48
00:03:41,260 --> 00:03:46,900
So yeah kind of nasty kind of long but thats just the name of the route here.

49
00:03:46,900 --> 00:03:49,270
Theres a lot of stuff involved in that route.

50
00:03:49,920 --> 00:03:50,850
OK.

51
00:03:51,330 --> 00:04:02,070
So inside the form all we need is an input type equals submit and then will give to a class of DTN ETN

52
00:04:02,520 --> 00:04:07,480
X s and BICI in danger.

53
00:04:07,500 --> 00:04:12,870
That should make it nice and red for us and then we can give it a value because it didn't do a button

54
00:04:12,870 --> 00:04:16,130
this time just to show you how to use an input type submit.

55
00:04:16,260 --> 00:04:22,470
You give it a value for the text on the actual button whereas if its a button element you have inner

56
00:04:22,480 --> 00:04:24,570
enraged him out the set but we dont have that here.

57
00:04:24,630 --> 00:04:27,790
So value equals let's just say do we.

58
00:04:28,830 --> 00:04:31,310
OK let's refresh.

59
00:04:32,100 --> 00:04:38,490
So first thing I notice we have that same issue that we did with the campground where the delete button

60
00:04:38,490 --> 00:04:41,680
is in a form so that sent it to the next line.

61
00:04:41,970 --> 00:04:45,210
Luckily we already have that style sheet that we defined.

62
00:04:45,490 --> 00:04:54,120
If we go into public stylesheets main see assess we have this delete form to set display to in line

63
00:04:54,120 --> 00:04:54,590
.

64
00:04:54,750 --> 00:05:03,660
So if we try that now 9 specked we also set that form have display in line.

65
00:05:04,650 --> 00:05:07,840
You can see that the buttons behave the way that I want them to.

66
00:05:08,040 --> 00:05:08,960
So we'll do that.

67
00:05:08,960 --> 00:05:15,720
But with our CSSA file and we don't actually have to write anything new in our CSSA file just copy this

68
00:05:15,720 --> 00:05:23,900
ID and also assign it to this forum just like that.

69
00:05:23,970 --> 00:05:26,370
Now our form is nice and in line.

70
00:05:26,580 --> 00:05:30,300
The last thing that we need to do is actually implement our route.

71
00:05:30,420 --> 00:05:32,210
So let's see what happens.

72
00:05:32,880 --> 00:05:33,380
Good.

73
00:05:33,390 --> 00:05:36,060
We get this is the destroy common route.

74
00:05:36,060 --> 00:05:40,530
Now we actually need to destroy the comment so in comments yes.

75
00:05:40,590 --> 00:05:42,110
Get rid of us.

76
00:05:42,180 --> 00:05:51,650
We're going to have a comments fined by id and remove members of the ID goes first.

77
00:05:52,230 --> 00:05:58,100
So that's request harams dot comments ID.

78
00:05:58,140 --> 00:05:59,520
This one here.

79
00:06:00,650 --> 00:06:07,590
And just to hammer this home one more time this I.D. is what we're talking about.

80
00:06:07,590 --> 00:06:11,860
So we have the campground I.D. comment slash comment Id.

81
00:06:12,390 --> 00:06:13,580
OK.

82
00:06:14,580 --> 00:06:17,970
Next thing we'll do have a callback function error

83
00:06:20,700 --> 00:06:27,330
and then all that we can do in here is just check if there's an error just to arrest out redirect back

84
00:06:27,330 --> 00:06:29,500
again.

85
00:06:31,560 --> 00:06:38,070
And if there's not an error eventually will send a message and eventually I actually mean it just a

86
00:06:38,070 --> 00:06:42,490
few videos from now we're getting really close to this one but will send a message that says comment

87
00:06:42,490 --> 00:06:43,960
successfully removed.

88
00:06:44,130 --> 00:06:47,300
For now redstart redirect.

89
00:06:47,460 --> 00:06:51,560
And after we delete a comment it makes sense to go back to the show page.

90
00:06:51,630 --> 00:06:57,960
We could do redstart redirect back as well assuming that the user came from the show page which is the

91
00:06:57,960 --> 00:07:03,510
only place we'll have a delete button for a comment but technically someone could send that request

92
00:07:03,510 --> 00:07:07,650
from something like Postman and also just so you get more practice.

93
00:07:07,650 --> 00:07:10,350
I'm going to write the show page redirect from scratch.

94
00:07:10,350 --> 00:07:21,330
So slash campgrounds slash ID and that ID is request up harams dot Id not not comment Id But Id which

95
00:07:21,330 --> 00:07:23,410
is the campground ID.

96
00:07:23,410 --> 00:07:27,980
We have a lot of IDS going on but the first one on our route is for the campground.

97
00:07:28,020 --> 00:07:32,900
So this should send us back to the show page and we'll hopefully see that that comment was deleted.

98
00:07:33,480 --> 00:07:38,560
OK so let's give it a shot restart.

99
00:07:40,470 --> 00:07:43,380
I don't have to be logged in right now to do this.

100
00:07:43,500 --> 00:07:47,390
So if I click delete you can see it's gone.

101
00:07:47,400 --> 00:07:49,260
I do have to be logged in to add one.

102
00:07:49,320 --> 00:07:55,230
So let me log back in password.

103
00:07:56,850 --> 00:08:03,210
Let's add a new comment to this one.

104
00:08:03,210 --> 00:08:05,850
Make sure we can still edit it.

105
00:08:07,170 --> 00:08:09,550
Let's add one more.

106
00:08:10,050 --> 00:08:14,250
Very original comments and lots to eat one.

107
00:08:14,310 --> 00:08:14,930
Perfect.

108
00:08:14,940 --> 00:08:16,070
It's working.

109
00:08:16,290 --> 00:08:21,140
So that's all we need to do to mend steps defining the destroy route.

110
00:08:21,220 --> 00:08:27,900
We actually add in the logic find by id and remove and the other important step is adding that button

111
00:08:27,990 --> 00:08:35,130
which is actually a form because we have to send a post request which is actually disguised as a delete

112
00:08:35,130 --> 00:08:40,710
request instead of a route file so our forms in the post we added an underscore method equals delete

113
00:08:41,370 --> 00:08:49,230
method override is expecting that underscore method and that triggers our delete route here rather than

114
00:08:50,340 --> 00:08:54,490
raftered up posts which is what it would trigger otherwise without method override.

115
00:08:55,090 --> 00:08:55,830
OK.

116
00:08:56,100 --> 00:09:01,950
Next up we're going to go and add in the code for authorization so that you first of all have to be

117
00:09:01,950 --> 00:09:07,510
logged in to edit and delete something and it has to belong to you to edit and delete a comment.

118
00:09:07,590 --> 00:09:11,880
So you can't just arbitrarily go and delete all the comments on an application.

119
00:09:12,360 --> 00:09:12,720
OK.

120
00:09:12,750 --> 00:09:13,890
So we'll do that next video
