1
00:00:00,360 --> 00:00:01,260
Welcome back.

2
00:00:01,260 --> 00:00:06,870
So we have one more route to finish up which is our destroy route which is how we can delete a particular

3
00:00:06,870 --> 00:00:09,350
blog or in this case a dog.

4
00:00:09,720 --> 00:00:16,080
So it's a delete request that's the verb we have to use our underscore method with method override to

5
00:00:16,080 --> 00:00:17,050
make that work.

6
00:00:17,490 --> 00:00:22,650
And then we also need to have an ID in the path because we're deleting a particular thing.

7
00:00:22,830 --> 00:00:27,630
And typically your delete route or your destroy route will delete something and then redirect you somewhere

8
00:00:27,630 --> 00:00:28,390
else.

9
00:00:28,440 --> 00:00:33,900
Usually the index because there is no show page to redirect you to if you've just deleted the thing

10
00:00:34,080 --> 00:00:35,690
that you would redirect to.

11
00:00:35,700 --> 00:00:39,390
So let's go back and let's start by defining her route.

12
00:00:40,080 --> 00:00:43,930
So the last one I know it's been a little bit of a marathon.

13
00:00:44,070 --> 00:00:54,440
So we have our delete route and it should be an app out delete slash blog slash colon ID at our Colback

14
00:00:54,450 --> 00:01:06,590
in and both just do what we did with Update response that send the we destroy route.

15
00:01:07,170 --> 00:01:11,660
Let's say you have reached the Detroit route.

16
00:01:12,660 --> 00:01:15,890
Great save.

17
00:01:16,080 --> 00:01:21,570
And just like with the update or really with any of these routes this doesn't have to be a delete request

18
00:01:21,570 --> 00:01:27,570
in order to delete something like I could make this a get request that's just blog slash ID slash delete

19
00:01:28,110 --> 00:01:30,580
or remove or whatever I want.

20
00:01:30,870 --> 00:01:36,870
It's just if we're following restful routing conventions and needs to be a delete request OK.

21
00:01:36,930 --> 00:01:41,920
So after that we blog session ID inside of here we're just sending back.

22
00:01:41,940 --> 00:01:43,660
You are free to distort.

23
00:01:43,980 --> 00:01:50,610
Now we need a way to hit that route so we can go to our show template and let's just do it underneath

24
00:01:51,600 --> 00:01:56,830
the post here will have a button that just says delete.

25
00:01:57,060 --> 00:02:04,160
So we'll go back to show and just under here to make this work we need to add in a form actually.

26
00:02:04,170 --> 00:02:13,410
So we need another form just like we did with update and we can start simple method is post and that's

27
00:02:13,410 --> 00:02:16,470
because it has to be using method override.

28
00:02:16,470 --> 00:02:23,950
It needs to be a post request but then we need to send or action which is equal to you or else a site

29
00:02:23,970 --> 00:02:24,750
blog.

30
00:02:24,990 --> 00:02:26,670
And we need the ID there.

31
00:02:26,900 --> 00:02:30,890
And that needs to be added in with Ejaz tags logged out.

32
00:02:30,900 --> 00:02:37,890
Underscore Id remember blog is what we're passing in when we find it in the database using find by ID

33
00:02:37,890 --> 00:02:38,160
.

34
00:02:38,160 --> 00:02:39,860
So we're passing that through.

35
00:02:41,100 --> 00:02:47,970
But this isn't enough because making a post request to blog slash logged that ID is not going to be

36
00:02:47,970 --> 00:02:48,970
our daily route.

37
00:02:48,990 --> 00:02:53,690
We need to add an underscore method equals delete.

38
00:02:54,480 --> 00:02:58,890
I still think it's weird after years of doing this it's kind of annoying but it's just what you have

39
00:02:58,890 --> 00:02:59,260
to do.

40
00:02:59,340 --> 00:03:03,210
So under schwere method is delete then inside the form.

41
00:03:03,210 --> 00:03:05,700
All we need is a single button to submit the form.

42
00:03:06,060 --> 00:03:14,430
So I add in a button tag and our button will just say delete just delete and or add in a class here

43
00:03:14,610 --> 00:03:17,470
just to make it look a little nicer using semantic.

44
00:03:17,670 --> 00:03:23,910
We can just do a UI read basic button and you can pick your own color if you want or ensure you aren't

45
00:03:23,910 --> 00:03:25,380
one of the inverted ones.

46
00:03:25,380 --> 00:03:26,260
Or a custom color.

47
00:03:26,280 --> 00:03:29,480
But this is fine for me red basic button that says delete.

48
00:03:29,550 --> 00:03:32,860
It will submit this form sends a request.

49
00:03:32,880 --> 00:03:34,240
Technically a post request.

50
00:03:34,320 --> 00:03:39,870
But our method override sees this and it treats it as a delete request which will hopefully trigger

51
00:03:40,230 --> 00:03:40,670
this code.

52
00:03:40,670 --> 00:03:43,780
Here we should see you have reached the destroy route.

53
00:03:44,120 --> 00:03:48,120
OK let's give it a shot.

54
00:03:48,840 --> 00:03:51,180
So we have a button click.

55
00:03:51,450 --> 00:03:51,990
Great.

56
00:03:52,140 --> 00:03:55,170
I get the rest out send you freaks the destroy route.

57
00:03:55,610 --> 00:03:58,200
So the last thing we have to do is actually destroy it.

58
00:03:58,200 --> 00:04:00,750
Instead of that route.

59
00:04:00,780 --> 00:04:02,730
So again it's a two step process.

60
00:04:02,780 --> 00:04:05,010
There are two main things we do.

61
00:04:05,010 --> 00:04:09,780
Destroy the log and then redirect somewhere.

62
00:04:10,500 --> 00:04:11,600
So to destroy.

63
00:04:11,850 --> 00:04:14,660
We're fortunate there's another nice method.

64
00:04:14,670 --> 00:04:24,300
Fine by id and remove and that needs an ID which again is request up harams that ID and then our callback

65
00:04:25,080 --> 00:04:29,760
which just takes the error because there is no data that will want to do anything with coming back.

66
00:04:29,760 --> 00:04:31,840
If we delete something it's gone.

67
00:04:32,130 --> 00:04:35,370
So we'll leave it at that and check if there's an error.

68
00:04:35,370 --> 00:04:39,900
If there is we'll handle it relatively poorly.

69
00:04:40,020 --> 00:04:41,490
We won't give any messages or anything.

70
00:04:41,490 --> 00:04:47,880
I'll just redirect to sashed Largs Again else will actually do the same thing.

71
00:04:48,450 --> 00:04:53,310
But I will leave this statement in here just because it's good practice to check for an error even if

72
00:04:53,310 --> 00:04:54,380
we're doing the same thing.

73
00:04:54,400 --> 00:04:56,310
There's identical outcomes.

74
00:04:56,760 --> 00:04:59,000
OK so let's see what happens.

75
00:04:59,050 --> 00:05:07,890
Now redirecting the slash blogs after we find by id and remove a particular blog Let's fire it up.

76
00:05:08,310 --> 00:05:13,500
Let's go back to the root path and let's find one that's worth reading.

77
00:05:13,500 --> 00:05:17,100
So I think one of these is missing a title.

78
00:05:17,430 --> 00:05:18,640
Yes this one here.

79
00:05:18,840 --> 00:05:25,870
So let's remove that quick undelete and hopefully.

80
00:05:25,930 --> 00:05:27,390
Yup there we go what's missing.

81
00:05:27,570 --> 00:05:28,800
So let's delete this one again.

82
00:05:28,810 --> 00:05:31,040
It has a gigantic image.

83
00:05:31,400 --> 00:05:32,910
We need to go to the show page.

84
00:05:33,060 --> 00:05:36,800
Read more and then click on that.

85
00:05:37,980 --> 00:05:40,220
And that's deleted as well.

86
00:05:40,260 --> 00:05:43,810
Now we're just back to these adorable bulldog's.

87
00:05:43,890 --> 00:05:46,810
All right so that's all there is to restful routes.

88
00:05:46,830 --> 00:05:47,330
It's a lot.

89
00:05:47,340 --> 00:05:52,740
I know this is something that when I had teachers in person when we have three months or six months

90
00:05:53,460 --> 00:05:56,510
we hit this hard for two plus weeks.

91
00:05:56,790 --> 00:06:02,460
I would have to build an app for dogs and then repeat the same thing for something equally boring like

92
00:06:02,460 --> 00:06:07,810
cats and users and friends and photos and you just get used to this pattern.

93
00:06:07,950 --> 00:06:12,510
And actually when the exercise is I really recommend you do that is not going to be all that fun or

94
00:06:12,510 --> 00:06:19,070
glamorous really is just to redo what we just did but with another thing.

95
00:06:19,080 --> 00:06:20,410
So not a blog.

96
00:06:20,550 --> 00:06:21,380
Maybe a dog.

97
00:06:21,390 --> 00:06:22,440
That's fine.

98
00:06:22,440 --> 00:06:23,940
Maybe you do it for a book.

99
00:06:24,000 --> 00:06:25,790
Maybe you do it for a movie it doesn't matter.

100
00:06:25,950 --> 00:06:29,550
But just something that you have all seven routes for.

101
00:06:29,580 --> 00:06:32,630
This is really important that you try and get comfortable with.

102
00:06:32,790 --> 00:06:38,250
We'll be using it all the time you know I'll be hitting it pretty hard and reiterating as we go.

103
00:06:38,520 --> 00:06:42,070
But if you wanted to start building your own apps and that's one of your goals.

104
00:06:42,510 --> 00:06:45,330
Memorize this type up the table from scratch.

105
00:06:45,330 --> 00:06:50,850
It's good practice to use a bootstrap table to the last small modification will make is to add an edit

106
00:06:50,880 --> 00:06:53,180
link to the show page.

107
00:06:53,220 --> 00:06:55,740
Right now we don't have a nice way of getting to it.

108
00:06:55,770 --> 00:06:57,360
So very simple.

109
00:06:57,360 --> 00:07:02,440
Just make another button just like we have with our delete.

110
00:07:02,580 --> 00:07:03,530
So I'll just copy that.

111
00:07:03,540 --> 00:07:06,450
Except it needs to be an anchor tag.

112
00:07:07,440 --> 00:07:11,760
Because we're not sending a form or not sending a post request just to get request.

113
00:07:11,820 --> 00:07:22,440
So a breath should be on just copy this blog's slash ID slash at it and this should just say at it and

114
00:07:22,440 --> 00:07:23,400
we're missing a quote.

115
00:07:23,430 --> 00:07:24,750
We have one too many quotes.

116
00:07:24,750 --> 00:07:25,520
There we go.

117
00:07:25,680 --> 00:07:31,320
Let's add in our class equal to and we'll do you.

118
00:07:31,730 --> 00:07:33,510
Let's do orange.

119
00:07:33,840 --> 00:07:42,930
Basic button save are fresh and we have are at it but you will notice that they're on different lines

120
00:07:42,930 --> 00:07:43,260
.

121
00:07:43,260 --> 00:07:49,120
The reason for that is that forms by default are not in line elements like an anchor tags.

122
00:07:49,260 --> 00:07:58,650
So if we inspect and select the form not the button but the form and I give it display in line.

123
00:07:58,650 --> 00:08:01,740
Notice that it jumps up like that which is probably what we want.

124
00:08:02,160 --> 00:08:08,070
So to do that there's no easy way as far as I know using semantic to do it without writing your own

125
00:08:08,100 --> 00:08:09,220
success.

126
00:08:09,240 --> 00:08:14,120
So what we'll do is just to find something in our CSSA file.

127
00:08:14,190 --> 00:08:20,780
So we open up after upsy assess and we'll just find an ID called Delete form.

128
00:08:21,030 --> 00:08:28,950
We'll just call it delete and this the styles for that will just be display in line.

129
00:08:28,950 --> 00:08:33,130
Now we just need to go over to our form which is right here.

130
00:08:33,300 --> 00:08:37,960
Just give it id equals the we and we should be good to go now.

131
00:08:38,070 --> 00:08:42,490
If I refresh it stays exactly the same.

132
00:08:42,720 --> 00:08:43,320
Perfect.

133
00:08:43,320 --> 00:08:47,280
So still month the most stylish not the most beautiful thing but it works for this.

134
00:08:47,340 --> 00:08:48,960
We're not going for anything crazy.

135
00:08:49,050 --> 00:08:49,440
Great.

136
00:08:49,470 --> 00:08:51,250
So the next video is optional.

137
00:08:51,390 --> 00:08:54,480
We're going to talk about styling the home page a bit.

138
00:08:54,660 --> 00:08:55,880
You guessed it it needs some work.

139
00:08:55,980 --> 00:09:01,290
And I'll talk about sanitizing the input that's coming from the user so that they can't do anything

140
00:09:01,290 --> 00:09:07,100
malicious they can't write a script tag that will run code when we display the post.

141
00:09:07,830 --> 00:09:08,320
OK.

142
00:09:08,390 --> 00:09:09,450
So in the next video
