Now let's go and set up the post route where we can submit the form to.
So I'll do that just below this.
And that's going to be apt at Post and The URL is slash campgrounds slash idea again slash comments
and that's it.
And we'll add in our requests and response to Colback and didn't hear we need to do a few things we
need to look up campground using ID then we need to create new comments.
Then we want to connect new comments to campground.
And then lastly we want to redirect somewhere and it would make sense to redirect back to the show page
of the campground that we're already on.
So a redirect to campground show page just like that.
OK.
Let's begin by looking up the campground using the ID and remember that's inside it for Quest dot harams
that ID.
So we'll do a campground find by Id just like we did up here.
Request Amzah ID and then our callback function with the error and the campground and then we need this
here and then our closing print to see.
And then in here what's it complaining about we're not using the error.
So we'll handle the error.
Constable log our or otherwise then we're going to move on and do all this code and if we can't find
the campground that they're adding a comment to then we have a problem.
So we won't do any of this but we could do instead just redirect back to slash campgrounds which is
pretty of noxious from a user's point of view.
But at least the app won't stall.
We're going to redirect somewhere eventually we'll be showing an error message explaining the problem
.
OK.
So redirect then needs to be rez redirect and then inside the else we're now going to create the new
comment.
So that's done with comments that create.
But before we do that we commented out and I want to show you what I was talking about when I introduced
this new way of adding the name attribute.
But rejoined things regroup them together under the name comment.
So we'll go back in here and let's just do a that log request dot body dot comments and save restart
the server and we'll go visit sosh campgrounds.
Click on one of them and then go do slash comments slash new and then we'll submit a comment.
This will be Ho ho ho from Santa.
Just like that.
Click submit and this request isn't going to actually end up seeing anything.
It's going to time out eventually because we haven't handled it properly.
We're not redirecting we're only redirecting if we can't find the campground.
Otherwise we're not doing anything.
But more importantly if we look at what request up body duck comment is you can see it's this pre-made
object that has everything we need to make a new comment.
So we don't have to sit here and do the VAR text equals request body text and then var author and so
on we can just use request up body document.
So we'll now create comments that create and instead of passing in that object we just pass and request
that body document which has those two pieces already in there.
And then our callback and we'll have our error and then our comment OK.
And then as always going to check if there's an error and we'll just do a simple con. log again.
And if it's all good then what we need to do is associate that comment to the campground and all we
have to do to make that work is do campground which is this variable here.
It's referring to the campground that we found with this ID you are well Dot comments.
Push comments which is the comment that just came back from the database that we created and then we
need to do a campground save for campground that save just like that and then we'll redirect.
So we'll do a rez that redirects to the show page of the campground and that show page again is going
to look like Slash campgrounds slash ID.
So we need to build that string.
It's a redstart redirect slash campgrounds.
But we need to put the idea in there.
So slash floods that's going to be campground dot underscore.
Id just like that.
OK.
So we're almost there.
This will hopefully redirect us to the right route so we can see the comment that was created but we
have a problem which is that comment is not defined and that's because this is the first time we're
actually using comment inside this app inside the US even though we are already accessing comments.
We never had to use the comment model because we were accessing comments off of the campground model
automatically in the template.
What we need to do is require comment appear and that's equal to require dot slash models slash comment
and save the comma and it now it should be good to go.
Let's restart those syntax errors.
Always a good sign.
I need to go back.
Here's the time out that I got when I never sent a response and never redirected or never rendered anything
.
Poor cloud 9 sent back their own message that just said something went wrong.
OK.
Slash campgrounds.
Now we'll click on one.
Now we'll go to slash comments slash new.
Here's our comment form and we'll do expect to Trona them.
And that's from Harry Potter.
Submit and what do you know.
We have a second comment that a down here the very last thing we'll do is add a simple button that will
take us to the new comment page rather than having to type that really long Lincoln manually.
We'll just add a button and that's on the show template and we'll just do it right before the comment
and we'll just make that an anchor tag inside of a paragraph so that it's spaced a little bit more.
So paragraph tag.
And then we want an anchor tag with each ref equal to slash campgrounds.
And then we need the ID and that ID is a campground ID.
So I'm just going to steal this and put it right in there and change it to be it underscore ID slash
comments and needs to go in the quotes slash comments slash new.
And then the text of the link should just be add new comment and we'll make it a bootstrap button.
So class is equal to let's do between DTN success which will make it green.
All right.
Let's test this out.
Start the server over again.
Let's go back to camp ground click on one of these.
We have add new comment.
Let's try adding just jibberish comment submit.
There we go.
So we have that functionality down.
And let's just verify that it works on other comments as well.
So let's go back to the campground and add on to canyon floor.
And this is jibberish again and it works great.
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
do my best to do it in 30 seconds.
So we started by talking about nested routes or we have campground slash ID and then comments and the
routes for comments are added after that because that comment is dependent on a campground and its particular
ID.
So we set those two routes up.
We moved our templates.
So now we have a campground directory and a common directory we had to change some of the linking and
especially linking to the Parshall's.
And then the bulk of what we did was down here in our routes.
So we created the new and create comment routes both of them we have to look up the correct campground
first.
Once we look up the campground on new all we do is render the form but once we look up the campground
on create what we actually had to do was then create the comment and then push that comment into the
campground and save.
And then we redirected back to the campground show page.
So a lot of changes lots of new stuff.
If you want to go back and compare v3 is the most recent version that doesn't have any of this new material
.
And the last important thing we have left to do around comments is just adding some styling so that
the show page looks a little bit nicer than it currently does and that the comments look a little better
as well.