1
00:00:00,690 --> 00:00:04,500
Now let's go and set up the post route where we can submit the form to.

2
00:00:05,130 --> 00:00:07,500
So I'll do that just below this.

3
00:00:08,070 --> 00:00:16,200
And that's going to be apt at Post and The URL is slash campgrounds slash idea again slash comments

4
00:00:16,590 --> 00:00:18,750
and that's it.

5
00:00:18,750 --> 00:00:24,690
And we'll add in our requests and response to Colback and didn't hear we need to do a few things we

6
00:00:24,690 --> 00:00:33,750
need to look up campground using ID then we need to create new comments.

7
00:00:33,750 --> 00:00:39,220
Then we want to connect new comments to campground.

8
00:00:39,660 --> 00:00:44,820
And then lastly we want to redirect somewhere and it would make sense to redirect back to the show page

9
00:00:45,150 --> 00:00:46,960
of the campground that we're already on.

10
00:00:47,130 --> 00:00:54,010
So a redirect to campground show page just like that.

11
00:00:54,350 --> 00:00:54,870
OK.

12
00:00:55,290 --> 00:01:02,190
Let's begin by looking up the campground using the ID and remember that's inside it for Quest dot harams

13
00:01:02,340 --> 00:01:03,470
that ID.

14
00:01:03,510 --> 00:01:09,380
So we'll do a campground find by Id just like we did up here.

15
00:01:09,950 --> 00:01:17,580
Request Amzah ID and then our callback function with the error and the campground and then we need this

16
00:01:17,580 --> 00:01:20,620
here and then our closing print to see.

17
00:01:20,880 --> 00:01:24,120
And then in here what's it complaining about we're not using the error.

18
00:01:24,270 --> 00:01:26,210
So we'll handle the error.

19
00:01:26,480 --> 00:01:34,380
Constable log our or otherwise then we're going to move on and do all this code and if we can't find

20
00:01:34,380 --> 00:01:38,480
the campground that they're adding a comment to then we have a problem.

21
00:01:38,730 --> 00:01:45,720
So we won't do any of this but we could do instead just redirect back to slash campgrounds which is

22
00:01:45,720 --> 00:01:47,820
pretty of noxious from a user's point of view.

23
00:01:47,820 --> 00:01:49,780
But at least the app won't stall.

24
00:01:49,780 --> 00:01:54,180
We're going to redirect somewhere eventually we'll be showing an error message explaining the problem

25
00:01:54,200 --> 00:01:54,540
.

26
00:01:54,820 --> 00:01:55,070
OK.

27
00:01:55,080 --> 00:02:01,230
So redirect then needs to be rez redirect and then inside the else we're now going to create the new

28
00:02:01,230 --> 00:02:02,000
comment.

29
00:02:02,250 --> 00:02:05,340
So that's done with comments that create.

30
00:02:05,820 --> 00:02:11,220
But before we do that we commented out and I want to show you what I was talking about when I introduced

31
00:02:11,220 --> 00:02:13,690
this new way of adding the name attribute.

32
00:02:13,860 --> 00:02:17,690
But rejoined things regroup them together under the name comment.

33
00:02:17,790 --> 00:02:27,480
So we'll go back in here and let's just do a that log request dot body dot comments and save restart

34
00:02:27,480 --> 00:02:32,670
the server and we'll go visit sosh campgrounds.

35
00:02:33,030 --> 00:02:40,150
Click on one of them and then go do slash comments slash new and then we'll submit a comment.

36
00:02:40,160 --> 00:02:43,980
This will be Ho ho ho from Santa.

37
00:02:44,130 --> 00:02:45,160
Just like that.

38
00:02:45,240 --> 00:02:49,790
Click submit and this request isn't going to actually end up seeing anything.

39
00:02:49,830 --> 00:02:53,520
It's going to time out eventually because we haven't handled it properly.

40
00:02:53,520 --> 00:02:57,280
We're not redirecting we're only redirecting if we can't find the campground.

41
00:02:57,450 --> 00:02:59,210
Otherwise we're not doing anything.

42
00:02:59,310 --> 00:03:04,710
But more importantly if we look at what request up body duck comment is you can see it's this pre-made

43
00:03:04,710 --> 00:03:07,700
object that has everything we need to make a new comment.

44
00:03:08,160 --> 00:03:18,270
So we don't have to sit here and do the VAR text equals request body text and then var author and so

45
00:03:18,270 --> 00:03:22,210
on we can just use request up body document.

46
00:03:22,590 --> 00:03:31,260
So we'll now create comments that create and instead of passing in that object we just pass and request

47
00:03:31,770 --> 00:03:36,710
that body document which has those two pieces already in there.

48
00:03:36,960 --> 00:03:44,910
And then our callback and we'll have our error and then our comment OK.

49
00:03:45,060 --> 00:03:54,050
And then as always going to check if there's an error and we'll just do a simple con. log again.

50
00:03:54,930 --> 00:04:02,700
And if it's all good then what we need to do is associate that comment to the campground and all we

51
00:04:02,700 --> 00:04:07,930
have to do to make that work is do campground which is this variable here.

52
00:04:08,080 --> 00:04:15,390
It's referring to the campground that we found with this ID you are well Dot comments.

53
00:04:15,450 --> 00:04:22,050
Push comments which is the comment that just came back from the database that we created and then we

54
00:04:22,050 --> 00:04:32,270
need to do a campground save for campground that save just like that and then we'll redirect.

55
00:04:32,370 --> 00:04:39,420
So we'll do a rez that redirects to the show page of the campground and that show page again is going

56
00:04:39,420 --> 00:04:44,690
to look like Slash campgrounds slash ID.

57
00:04:44,760 --> 00:04:46,650
So we need to build that string.

58
00:04:46,650 --> 00:04:49,920
It's a redstart redirect slash campgrounds.

59
00:04:50,370 --> 00:04:52,010
But we need to put the idea in there.

60
00:04:52,170 --> 00:04:57,950
So slash floods that's going to be campground dot underscore.

61
00:04:58,050 --> 00:05:00,060
Id just like that.

62
00:05:00,720 --> 00:05:01,020
OK.

63
00:05:01,030 --> 00:05:02,490
So we're almost there.

64
00:05:02,490 --> 00:05:07,560
This will hopefully redirect us to the right route so we can see the comment that was created but we

65
00:05:07,560 --> 00:05:12,810
have a problem which is that comment is not defined and that's because this is the first time we're

66
00:05:12,810 --> 00:05:19,530
actually using comment inside this app inside the US even though we are already accessing comments.

67
00:05:19,590 --> 00:05:25,170
We never had to use the comment model because we were accessing comments off of the campground model

68
00:05:25,290 --> 00:05:26,780
automatically in the template.

69
00:05:26,970 --> 00:05:38,640
What we need to do is require comment appear and that's equal to require dot slash models slash comment

70
00:05:39,240 --> 00:05:43,930
and save the comma and it now it should be good to go.

71
00:05:44,250 --> 00:05:47,250
Let's restart those syntax errors.

72
00:05:47,250 --> 00:05:49,010
Always a good sign.

73
00:05:49,080 --> 00:05:50,090
I need to go back.

74
00:05:50,130 --> 00:05:55,080
Here's the time out that I got when I never sent a response and never redirected or never rendered anything

75
00:05:55,080 --> 00:05:55,610
.

76
00:05:55,740 --> 00:05:59,660
Poor cloud 9 sent back their own message that just said something went wrong.

77
00:06:00,230 --> 00:06:01,190
OK.

78
00:06:01,300 --> 00:06:02,590
Slash campgrounds.

79
00:06:02,760 --> 00:06:05,790
Now we'll click on one.

80
00:06:05,790 --> 00:06:09,610
Now we'll go to slash comments slash new.

81
00:06:10,050 --> 00:06:16,130
Here's our comment form and we'll do expect to Trona them.

82
00:06:16,320 --> 00:06:18,110
And that's from Harry Potter.

83
00:06:18,180 --> 00:06:20,790
Submit and what do you know.

84
00:06:20,790 --> 00:06:25,470
We have a second comment that a down here the very last thing we'll do is add a simple button that will

85
00:06:25,470 --> 00:06:30,330
take us to the new comment page rather than having to type that really long Lincoln manually.

86
00:06:30,390 --> 00:06:36,540
We'll just add a button and that's on the show template and we'll just do it right before the comment

87
00:06:36,810 --> 00:06:42,000
and we'll just make that an anchor tag inside of a paragraph so that it's spaced a little bit more.

88
00:06:42,060 --> 00:06:43,170
So paragraph tag.

89
00:06:43,320 --> 00:06:50,540
And then we want an anchor tag with each ref equal to slash campgrounds.

90
00:06:50,910 --> 00:06:55,510
And then we need the ID and that ID is a campground ID.

91
00:06:55,530 --> 00:07:04,590
So I'm just going to steal this and put it right in there and change it to be it underscore ID slash

92
00:07:04,770 --> 00:07:10,330
comments and needs to go in the quotes slash comments slash new.

93
00:07:10,950 --> 00:07:18,750
And then the text of the link should just be add new comment and we'll make it a bootstrap button.

94
00:07:18,750 --> 00:07:26,900
So class is equal to let's do between DTN success which will make it green.

95
00:07:27,030 --> 00:07:27,360
All right.

96
00:07:27,360 --> 00:07:28,590
Let's test this out.

97
00:07:28,590 --> 00:07:30,250
Start the server over again.

98
00:07:30,680 --> 00:07:34,970
Let's go back to camp ground click on one of these.

99
00:07:35,100 --> 00:07:36,560
We have add new comment.

100
00:07:36,900 --> 00:07:40,320
Let's try adding just jibberish comment submit.

101
00:07:40,320 --> 00:07:41,180
There we go.

102
00:07:41,550 --> 00:07:43,220
So we have that functionality down.

103
00:07:43,470 --> 00:07:46,940
And let's just verify that it works on other comments as well.

104
00:07:46,950 --> 00:07:52,260
So let's go back to the campground and add on to canyon floor.

105
00:07:52,640 --> 00:07:57,330
And this is jibberish again and it works great.

106
00:07:57,660 --> 00:08:02,670
And as you know by now I like to end by doing a quick recap and we have a lot to recap here but I'll

107
00:08:02,670 --> 00:08:05,580
do my best to do it in 30 seconds.

108
00:08:05,580 --> 00:08:12,000
So we started by talking about nested routes or we have campground slash ID and then comments and the

109
00:08:12,000 --> 00:08:17,010
routes for comments are added after that because that comment is dependent on a campground and its particular

110
00:08:17,010 --> 00:08:17,990
ID.

111
00:08:18,000 --> 00:08:19,670
So we set those two routes up.

112
00:08:19,890 --> 00:08:21,270
We moved our templates.

113
00:08:21,270 --> 00:08:26,730
So now we have a campground directory and a common directory we had to change some of the linking and

114
00:08:26,810 --> 00:08:29,130
especially linking to the Parshall's.

115
00:08:30,600 --> 00:08:34,130
And then the bulk of what we did was down here in our routes.

116
00:08:34,530 --> 00:08:41,210
So we created the new and create comment routes both of them we have to look up the correct campground

117
00:08:41,240 --> 00:08:41,850
first.

118
00:08:42,060 --> 00:08:47,910
Once we look up the campground on new all we do is render the form but once we look up the campground

119
00:08:48,050 --> 00:08:54,540
on create what we actually had to do was then create the comment and then push that comment into the

120
00:08:54,540 --> 00:08:56,380
campground and save.

121
00:08:56,610 --> 00:09:00,060
And then we redirected back to the campground show page.

122
00:09:00,510 --> 00:09:02,390
So a lot of changes lots of new stuff.

123
00:09:02,490 --> 00:09:07,710
If you want to go back and compare v3 is the most recent version that doesn't have any of this new material

124
00:09:07,710 --> 00:09:08,490
.

125
00:09:08,520 --> 00:09:13,350
And the last important thing we have left to do around comments is just adding some styling so that

126
00:09:13,350 --> 00:09:17,700
the show page looks a little bit nicer than it currently does and that the comments look a little better

127
00:09:17,700 --> 00:09:18,420
as well.
