Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Start a free Basic 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
-
0:00
[MUSIC]
-
0:04
Our users can create pages, read pages, update pages, what's left?
-
0:10
Right, deleting pages.
-
0:11
For every resource you create,
-
0:13
there may come a time when you don't want it anymore.
-
0:15
You might not want prospective employers to see that page where you show off your
-
0:19
collection of giant robot toys.
-
0:21
So let's allow our users to delete pages.
-
0:25
Just as a browser is supposed to send get requests to get data from the server and
-
0:29
post requests to post new data on the server,
-
0:31
a browser sends delete requests to delete data.
-
0:34
Actually, as with Patch requests,
-
0:36
it's technically still a POST request coming from the browser, but
-
0:39
it has amount of parameters that indicate it should be treated as a delete
-
0:42
request and Rails converts to request type to delete internally.
-
0:46
Because it's a different request type,
-
0:47
that means we're going to need a different route.
-
0:49
Let's set one up in our routes.rb file here.
-
0:52
As with the post and patch routes,
-
0:54
there's no need to worry about priority order since there's no chance of
-
0:57
a delete request getting confused with GET requests no matter what its path is.
-
1:01
So we'll put the new route here at the bottom.
-
1:03
Since we're routing delete requests, we'll call the delete method.
-
1:07
As with all our other page routes, the path should start with /pages.
-
1:14
And as with the show edit and update routes,
-
1:16
we'll need a model ID to look up which record we're working with.
-
1:19
So we'll add an ID URL parameter /:id.
-
1:25
We'll route these delete requests to the page's controller's destroy method.
-
1:30
to: pages#destroy.
-
1:36
Of course,
-
1:36
the destroy action method doesn't exist on the page's controller yet.
-
1:40
Let's add it now.
-
1:41
Action methods on the controller have to be public.
-
1:44
So be sure the method definition goes above the private keyword or
-
1:47
Rails won't be able to call it.
-
1:52
So we'll define a destroy method.
-
1:56
We'll look up the page to destroy it using the id parameter from the URL,
-
2:00
just like we do for the show method, the create method, etc.
-
2:03
We'll assign it to an instance variable named page, and
-
2:07
we'll call the Page.find method and we need to pass it the ID of
-
2:12
the page object it should look up, which we're getting from the params[:id].
-
2:19
Model objects have a destroy method you can call to delete
-
2:21
that record from the database.
-
2:23
So, we simply call page.destroy.
-
2:28
Now we need a way to send the delete request.
-
2:30
We'll add a delete link to the show view for an individual page.
-
2:33
So we'll edit at views > pages > show.html.erb.
-
2:39
At the bottom, we'll embed some output right after the edit link.
-
2:45
Within that tag, we'll call link to and we'll set the link text to delete.
-
2:51
Then we'll pass it our page object.
-
2:53
The object's ID will be used in the path for the generated link.
-
2:57
Let's try this delete link out.
-
2:59
We'll load the show view for one of the pages we want to get rid of.
-
3:03
We can look at the generated HTML for the link in our browser's developer tools.
-
3:09
There it is, a link to pages followed by the ID of the page object.
-
3:13
Now let's close the developer tools and click the link.
-
3:19
It seems like nothing's happening.
-
3:20
Let's go to our terminal and check the Rails log to see what's going on.
-
3:24
We seen started GET /pages/6 over and over.
-
3:31
It looks like our link is being handled as a GET request, not a delete request.
-
3:35
And because a GET request with this path gets routed to the show action,
-
3:39
it's just taking us back to the same page again.
-
3:42
Our link is taking us to the same page we're already on.
-
3:45
We can fix this by changing the type of HTTP request that the link sends.
-
3:50
We'll just add a method keyword argument to link to with
-
3:55
the request type that we want to send as a value.
-
3:58
In this case, we want to send a delete request, so we'll use the symbol delete.
-
4:03
Note that the method argument name here doesn't refer to the method on
-
4:06
the controller.
-
4:07
That's named destroy, not delete.
-
4:09
This is the HTTP request method such as Get post or delete.
-
4:14
Let's save this, refresh our browser and try clicking the delete link again.
-
4:19
It looks like nothing's happening still, but if we look at the log in our terminal,
-
4:24
you'll see that Rails processed it as a DELETE request.
-
4:27
We can also see that it loaded the requested page and then deleted it.
-
4:33
We can confirm this if we go back to the index of all pages.
-
4:36
The page we were on is gone, as it should be.
-
4:39
Now we just need to update the page's controller to handle things a little more
-
4:42
gracefully when we click that link.
-
4:44
As with the create an update methods, we're going to send the browser a redirect
-
4:47
response so that it loads a different path.
-
4:50
In our delete method, right after we destroy the page objects,
-
4:53
we'll call redirect_to.
-
4:57
And we'll send it to the /pages path by calling the pages_path method.
-
5:03
Save that, and now when we visit a page and click the Delete link, we'll be
-
5:08
redirected back to the list of all pages and that page will be gone from the list.
-
5:13
You can now create, read, update and
-
5:15
delete instances of your page model, all from your browser.
-
5:18
You have a complete Rails resource.
-
5:20
Before we wrap up the course, though,
-
5:22
there's a couple more things we can do to make the code cleaner.
-
5:25
The first will let us clean up our routes.rb file quite a bit, and
-
5:28
we'll be showing you that next.
You need to sign up for Treehouse in order to download course files.
Sign up