Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Start a free Courses trial
to watch this video
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 upRelated 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