So we've come to the end of our RESTful and Resourceful routes. Only one to go: Delete.

URL HTTP Verb Action
/ GET index
/reviews/new GET new
/reviews POST create
/reviews/:id GET show
/reviews/:id DELETE Destroy

What the User Sees

As always, we start with what the users sees and does. So let's make a link to delete a review.

We can't set an <a> tag's method (it is always GET) so we are going to use a form to submit a DELETE request to our delete action path.

<!-- views/reviews-show.handlebars -->

<h1>{{review.title}}</h1>
<h2>{{review.movieTitle}}</h2>
<p>{{review.description}}</p>
<p><a href="/reviews/{{review._id}}/edit">Edit</a></p>
<!-- Delete button -->
<p><form method="POST" action="/reviews/{{review._id}}?_method=DELETE">
    <button type="submit">Delete</button>
</form></p>

Now we need a delete action route. After deleting the review, it should redirect to the home page (reviews-index).

// app.js
...
// DELETE
app.delete('/reviews/:id', function (req, res) {
  console.log("DELETE review")
  Review.findByIdAndRemove(req.params.id).then((review) => {
    res.redirect('/');
  }).catch((err) => {
    console.log(err.message);
  })
})

We did it! All RESTful routes are complete! It ain't pretty though... let's work on that next.

Feedback

If you have feedback on this tutorial or find any mistakes, please open issues on the GitHub Repository or comment below.

Summer academy

An iOS Development Summer Course

Design, code and launch your own app. Locations in San Francisco and Asia

Find your location

Product College

A computer science college

Graduate into a successful career as a founder or software engineer.

Learn more

Cookies on Make School's website

We have placed cookies on your device to ensure that we give you the best experience on our website.

This site uses cookies to deliver our services. By using our site, you acknowledge that you have read and understand our Cookie Policy, Privacy Policy, and our Terms of Service. Your use of Make School’s Products and Services is subject to these policies and terms.

Please note that Make School no longer supports Internet Explorer

We recommend upgrading to a modern web browser. Learn more