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
We can add new wiki pages, show pages we've added, and then update them. The only thing missing now is the ability to delete pages. Let's add that now.
If you'd like to learn more about CSS, check out our CSS Basics course.
[MUSIC]
0:00
We can add new wiki pages show pages
we've added and then update them.
0:04
The only thing missing now is
the ability to delete pages.
0:08
Let's add that now.
0:12
Our wiki.rb file has a page_content method
that loads the contents of a text file and
0:13
a save_content method that saves a file.
0:18
I've added another method called
delete_content which deletes a file from
0:22
the pages directory.
0:26
It simply calls the File.delete method
which takes the name of a file to delete.
0:27
If you launch a work space
from this video's page,
0:33
you'll get a wiki.rb file with this
method already set up for you.
0:34
This time I'll leave
testing the method up to.
0:39
If you want to experiment with delete
content, you can copy it to a separate
0:41
file and add a call to it with the name of
a file in your pages directory to delete.
0:44
You may need to refresh the sidebar
before you can see the changes.
0:48
Now we just need a way for users to call
this method with a particular page title.
0:52
Let's set up a button on each wiki
page that lets users send a request
0:56
to delete that page.
1:00
There's one more commonly used request
type that Sinatra recognizes and
1:01
that's delete request.
1:05
But as with put requests,
1:07
there's no way to send a delete requests
from plain HTML links or forms.
1:08
As with put the best way is
to create an HTML form and
1:12
use a hidden underscore method
parameter with a value of delete.
1:15
We won't add any text fields to this form,
1:19
it's just going to consist
of a single delete button.
1:21
In the show.erb template,
let's add a form element.
1:23
We're going to place it
within the div element so
1:29
that it's easier to style it later.
1:31
As before, the only requests type
supported by HTML are get and post, so
1:35
we'll set the form's
method attribute to post.
1:40
Then within the form, will add an input
element with the type of hidden.
1:44
It'll have a name of _method And
a value of delete.
1:53
That will cause Sinatra to delete
the form submission as delete request.
2:02
As with all the other requests types,
delete request need a path to act on.
2:06
We'll set that by adding an action
attribute to the form tag.
2:09
We'll give it the path of the wiki
page we're trying to delete.
2:14
That is a slash followed by the page
title which will embed using an erb tag.
2:17
As before, we can reuse the title instance
variable which is already used elsewhere
2:22
in the template.
2:26
Lastly, we'll need the button that users
will click to submit the requests.
2:27
Will add an input element
with a type of submit.
2:31
The value attribute determines
what the button will be labeled.
2:37
So we'll give it a value
of delete this page.
2:42
Let's start up our server and
run a preview.
2:46
And now let's view a wiki page.
2:51
We'll see a new Delete This Page
button at the bottom.
2:55
If we click that button,
3:00
we'll see Sinatra doesn't know this ditty
again because there's no matching route.
3:01
At the bottom of the page,
3:05
we'll see a recommendation to create
a route using a method named delete.
3:06
That means our form submission is
being treated as a delete request.
3:10
Now we just need to edit our wiki.rb
file and add a delete route.
3:15
Again, order doesn't matter.
3:19
There's no way a delete request
will accidentally be matched by
3:20
our route's forget, post or put requests.
3:23
Just as a method to set up a route for get
requests is called GET and the method for
3:27
post routes is called POST the method for
delete routes is called Delete.
3:31
So we'll add a call to that method now.
3:36
And you probably know what comes
next by now, the path to match.
3:37
We set up our form to submit to a path
of a slash followed by the current
3:42
page title.
3:45
So that's what will match here a slash
followed by a URL parameter for the title.
3:46
And of course we need a block to
invoke when the routers match.
3:52
In that block, what we'll have to do
is call our delete_content method
3:58
with the page title and it will delete
the appropriate text file for us.
4:01
So, we'll call delete_content and
give it the title from the URL parameter.
4:05
We'll also need to redirect
the browser to another page, so
4:12
we'll call the redirect method.
4:15
We'll just redirect them to the root path.
4:19
There's no need to call URI dot
escape on the redirect path this time
4:22
since there's no chance of it
containing an invalid character.
4:25
Save that.
4:29
Let's restart our server.
4:30
And go back to the home page.
4:36
We'll create a new page to test
our delete functionality out on.
4:37
We'll use the title of John Doe and
text of just some guy.
4:42
Submit it.
4:50
And if we go back to our workspace and
refresh the file sidebar,
4:51
We'll see John Doe.text here
in the page's directory.
4:57
If we go back to the new page and
5:00
click delete,
we'll be redirected back to the root path.
5:02
And if we refresh our workspace
sidebar again, we'll see if
5:08
the John Doe.text file has been deleted.
5:13
You need to sign up for Treehouse in order to download course files.
Sign up