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