Welcome back.
So we have another video about comments finishing up crud we're focusing on the D which is delete.
So to delete a comment we need to add in our route and we also need to add in the button which is actually
a form that will submit a request to that destroy route.
And this is pretty simple compared to edit and update only one route.
You don't need a crazy complex form.
You don't have to worry about a lot of the complications that we did with adding an update for campground
and in particular comments.
So the route that we need.
Let's take a look at how we did it for campground to start.
We scroll down slash ID and that's it.
But remember our route is actually slash campground slash ID as a delete request so we can add that
in campground destroy route is slash campgrounds slash ID as a delete request and our comments destroy
route is nested so it's a little bit longer.
It's slash campgrounds slash ID slash comments slash comments ID as a delete request.
So we're falling a restful route here.
Let's now go and implement this route.
We'll begin by going to our comment genius.
The routes file and we'll add in a comment that says comments destroy route.
And then we need our router dot delete and the route is just comment ID and that's it because that other
part has been prepended to it when we required it in the US and I've showed that a lot in last few videos
.
So I won't do that again but it's in the US where we require this file and then we need our request
in response standard call back here.
And what we'll do here eventually is find my ID and remove.
But for now we're just going to rest.
And this is the destroy comments route and will save.
Now let's try and hit this route.
So we'll go to our show form.
Let me start the server first.
What we want is a button right here.
Just look at it.
But let's make it red and then click on it and it sends a delete request to that route.
But remember it's not actually a delete request it's a post request because we can't make a delete request
from a form yet it's allegedly coming in the future.
That's the whole method override thing that's why we're using it.
We sent a post request with underscore method equal to to be.
So let's do that now in our show.
So it right below this button here this anchor tag will add in a form and it can't be just an anchor
tag because that only makes you get requests.
We needed a post.
So action it's going to be long again.
We can actually copy what we have above campground slash campground got ID slash comments slash comment
ID so we can copy that whole thing in here.
And we don't need slash edit.
We don't need to slash anything after the ID except we do need method underscore method equals delete
and then we also need method equals post.
So yeah kind of nasty kind of long but thats just the name of the route here.
Theres a lot of stuff involved in that route.
OK.
So inside the form all we need is an input type equals submit and then will give to a class of DTN ETN
X s and BICI in danger.
That should make it nice and red for us and then we can give it a value because it didn't do a button
this time just to show you how to use an input type submit.
You give it a value for the text on the actual button whereas if its a button element you have inner
enraged him out the set but we dont have that here.
So value equals let's just say do we.
OK let's refresh.
So first thing I notice we have that same issue that we did with the campground where the delete button
is in a form so that sent it to the next line.
Luckily we already have that style sheet that we defined.
If we go into public stylesheets main see assess we have this delete form to set display to in line
.
So if we try that now 9 specked we also set that form have display in line.
You can see that the buttons behave the way that I want them to.
So we'll do that.
But with our CSSA file and we don't actually have to write anything new in our CSSA file just copy this
ID and also assign it to this forum just like that.
Now our form is nice and in line.
The last thing that we need to do is actually implement our route.
So let's see what happens.
Good.
We get this is the destroy common route.
Now we actually need to destroy the comment so in comments yes.
Get rid of us.
We're going to have a comments fined by id and remove members of the ID goes first.
So that's request harams dot comments ID.
This one here.
And just to hammer this home one more time this I.D. is what we're talking about.
So we have the campground I.D. comment slash comment Id.
OK.
Next thing we'll do have a callback function error
and then all that we can do in here is just check if there's an error just to arrest out redirect back
again.
And if there's not an error eventually will send a message and eventually I actually mean it just a
few videos from now we're getting really close to this one but will send a message that says comment
successfully removed.
For now redstart redirect.
And after we delete a comment it makes sense to go back to the show page.
We could do redstart redirect back as well assuming that the user came from the show page which is the
only place we'll have a delete button for a comment but technically someone could send that request
from something like Postman and also just so you get more practice.
I'm going to write the show page redirect from scratch.
So slash campgrounds slash ID and that ID is request up harams dot Id not not comment Id But Id which
is the campground ID.
We have a lot of IDS going on but the first one on our route is for the campground.
So this should send us back to the show page and we'll hopefully see that that comment was deleted.
OK so let's give it a shot restart.
I don't have to be logged in right now to do this.
So if I click delete you can see it's gone.
I do have to be logged in to add one.
So let me log back in password.
Let's add a new comment to this one.
Make sure we can still edit it.
Let's add one more.
Very original comments and lots to eat one.
Perfect.
It's working.
So that's all we need to do to mend steps defining the destroy route.
We actually add in the logic find by id and remove and the other important step is adding that button
which is actually a form because we have to send a post request which is actually disguised as a delete
request instead of a route file so our forms in the post we added an underscore method equals delete
method override is expecting that underscore method and that triggers our delete route here rather than
raftered up posts which is what it would trigger otherwise without method override.
OK.
Next up we're going to go and add in the code for authorization so that you first of all have to be
logged in to edit and delete something and it has to belong to you to edit and delete a comment.
So you can't just arbitrarily go and delete all the comments on an application.
OK.
So we'll do that next video