Deleting Pages5:30 with Jay McGavren
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
You need to sign up for Treehouse in order to download course files.Sign up