This course will be retired on June 1, 2025.
Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll

- 2x 2x
- 1.75x 1.75x
- 1.5x 1.5x
- 1.25x 1.25x
- 1.1x 1.1x
- 1x 1x
- 0.75x 0.75x
- 0.5x 0.5x
Our users can create Pages, read Pages, update Pages... what's left? Oh, right, deleting Pages! For every resource you create, there may come a time when you don't want it any more. So, let's allow our users to delete Pages.
Deleting model records usually first requires providing a link that sends a DELETE request:
<%= link_to 'Delete', @page, method: :delete %>
You'll also need a route that sends the DELETE requests to the appropriate controller action:
delete '/pages/:id', to: 'pages#destroy'
The controller action usually performs the following operations:
def destroy
# Look up the model record to destroy
# based on the ID from the request path.
@page = Page.find(params[:id])
# Delete the matching record.
@page.destroy
# Redirect the browser to another
# appropriate page.
redirect_to pages_path
end
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up-
bashburn
7,611 Points1 Answer
-
MaryAnn Eleanya
8,626 Points1 Answer
-
Andrea Gomez
8,609 Points3 Answers
-
Karrtik Iyer
3,738 PointsInstead of method:delete in erb can we use delete_path or destory_path?
Posted by Karrtik IyerKarrtik Iyer
3,738 Points1 Answer
-
Andrew Schrenk
11,557 Points2 Answers
-
mahdi mostafa
9,750 Points0 Answers
-
Danish Saleem
7,965 Points1 Answer
View all discussions for this video
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up