Welcome back.
So we have one more route to finish up which is our destroy route which is how we can delete a particular
blog or in this case a dog.
So it's a delete request that's the verb we have to use our underscore method with method override to
make that work.
And then we also need to have an ID in the path because we're deleting a particular thing.
And typically your delete route or your destroy route will delete something and then redirect you somewhere
else.
Usually the index because there is no show page to redirect you to if you've just deleted the thing
that you would redirect to.
So let's go back and let's start by defining her route.
So the last one I know it's been a little bit of a marathon.
So we have our delete route and it should be an app out delete slash blog slash colon ID at our Colback
in and both just do what we did with Update response that send the we destroy route.
Let's say you have reached the Detroit route.
Great save.
And just like with the update or really with any of these routes this doesn't have to be a delete request
in order to delete something like I could make this a get request that's just blog slash ID slash delete
or remove or whatever I want.
It's just if we're following restful routing conventions and needs to be a delete request OK.
So after that we blog session ID inside of here we're just sending back.
You are free to distort.
Now we need a way to hit that route so we can go to our show template and let's just do it underneath
the post here will have a button that just says delete.
So we'll go back to show and just under here to make this work we need to add in a form actually.
So we need another form just like we did with update and we can start simple method is post and that's
because it has to be using method override.
It needs to be a post request but then we need to send or action which is equal to you or else a site
blog.
And we need the ID there.
And that needs to be added in with Ejaz tags logged out.
Underscore Id remember blog is what we're passing in when we find it in the database using find by ID
.
So we're passing that through.
But this isn't enough because making a post request to blog slash logged that ID is not going to be
our daily route.
We need to add an underscore method equals delete.
I still think it's weird after years of doing this it's kind of annoying but it's just what you have
to do.
So under schwere method is delete then inside the form.
All we need is a single button to submit the form.
So I add in a button tag and our button will just say delete just delete and or add in a class here
just to make it look a little nicer using semantic.
We can just do a UI read basic button and you can pick your own color if you want or ensure you aren't
one of the inverted ones.
Or a custom color.
But this is fine for me red basic button that says delete.
It will submit this form sends a request.
Technically a post request.
But our method override sees this and it treats it as a delete request which will hopefully trigger
this code.
Here we should see you have reached the destroy route.
OK let's give it a shot.
So we have a button click.
Great.
I get the rest out send you freaks the destroy route.
So the last thing we have to do is actually destroy it.
Instead of that route.
So again it's a two step process.
There are two main things we do.
Destroy the log and then redirect somewhere.
So to destroy.
We're fortunate there's another nice method.
Fine by id and remove and that needs an ID which again is request up harams that ID and then our callback
which just takes the error because there is no data that will want to do anything with coming back.
If we delete something it's gone.
So we'll leave it at that and check if there's an error.
If there is we'll handle it relatively poorly.
We won't give any messages or anything.
I'll just redirect to sashed Largs Again else will actually do the same thing.
But I will leave this statement in here just because it's good practice to check for an error even if
we're doing the same thing.
There's identical outcomes.
OK so let's see what happens.
Now redirecting the slash blogs after we find by id and remove a particular blog Let's fire it up.
Let's go back to the root path and let's find one that's worth reading.
So I think one of these is missing a title.
Yes this one here.
So let's remove that quick undelete and hopefully.
Yup there we go what's missing.
So let's delete this one again.
It has a gigantic image.
We need to go to the show page.
Read more and then click on that.
And that's deleted as well.
Now we're just back to these adorable bulldog's.
All right so that's all there is to restful routes.
It's a lot.
I know this is something that when I had teachers in person when we have three months or six months
we hit this hard for two plus weeks.
I would have to build an app for dogs and then repeat the same thing for something equally boring like
cats and users and friends and photos and you just get used to this pattern.
And actually when the exercise is I really recommend you do that is not going to be all that fun or
glamorous really is just to redo what we just did but with another thing.
So not a blog.
Maybe a dog.
That's fine.
Maybe you do it for a book.
Maybe you do it for a movie it doesn't matter.
But just something that you have all seven routes for.
This is really important that you try and get comfortable with.
We'll be using it all the time you know I'll be hitting it pretty hard and reiterating as we go.
But if you wanted to start building your own apps and that's one of your goals.
Memorize this type up the table from scratch.
It's good practice to use a bootstrap table to the last small modification will make is to add an edit
link to the show page.
Right now we don't have a nice way of getting to it.
So very simple.
Just make another button just like we have with our delete.
So I'll just copy that.
Except it needs to be an anchor tag.
Because we're not sending a form or not sending a post request just to get request.
So a breath should be on just copy this blog's slash ID slash at it and this should just say at it and
we're missing a quote.
We have one too many quotes.
There we go.
Let's add in our class equal to and we'll do you.
Let's do orange.
Basic button save are fresh and we have are at it but you will notice that they're on different lines
.
The reason for that is that forms by default are not in line elements like an anchor tags.
So if we inspect and select the form not the button but the form and I give it display in line.
Notice that it jumps up like that which is probably what we want.
So to do that there's no easy way as far as I know using semantic to do it without writing your own
success.
So what we'll do is just to find something in our CSSA file.
So we open up after upsy assess and we'll just find an ID called Delete form.
We'll just call it delete and this the styles for that will just be display in line.
Now we just need to go over to our form which is right here.
Just give it id equals the we and we should be good to go now.
If I refresh it stays exactly the same.
Perfect.
So still month the most stylish not the most beautiful thing but it works for this.
We're not going for anything crazy.
Great.
So the next video is optional.
We're going to talk about styling the home page a bit.
You guessed it it needs some work.
And I'll talk about sanitizing the input that's coming from the user so that they can't do anything
malicious they can't write a script tag that will run code when we display the post.
OK.
So in the next video