1 00:00:00,000 --> 00:00:04,364 [MUSIC] 2 00:00:04,364 --> 00:00:08,820 We can add new wiki pages show pages we've added and then update them. 3 00:00:08,820 --> 00:00:12,060 The only thing missing now is the ability to delete pages. 4 00:00:12,060 --> 00:00:13,500 Let's add that now. 5 00:00:13,500 --> 00:00:18,193 Our wiki.rb file has a page_content method that loads the contents of a text file and 6 00:00:18,193 --> 00:00:20,550 a save_content method that saves a file. 7 00:00:22,640 --> 00:00:26,240 I've added another method called delete_content which deletes a file from 8 00:00:26,240 --> 00:00:27,890 the pages directory. 9 00:00:27,890 --> 00:00:31,860 It simply calls the File.delete method which takes the name of a file to delete. 10 00:00:33,150 --> 00:00:34,935 If you launch a work space from this video's page, 11 00:00:34,935 --> 00:00:39,135 you'll get a wiki.rb file with this method already set up for you. 12 00:00:39,135 --> 00:00:41,305 This time I'll leave testing the method up to. 13 00:00:41,305 --> 00:00:44,755 If you want to experiment with delete content, you can copy it to a separate 14 00:00:44,755 --> 00:00:48,865 file and add a call to it with the name of a file in your pages directory to delete. 15 00:00:48,865 --> 00:00:52,900 You may need to refresh the sidebar before you can see the changes. 16 00:00:52,900 --> 00:00:56,980 Now we just need a way for users to call this method with a particular page title. 17 00:00:56,980 --> 00:01:00,250 Let's set up a button on each wiki page that lets users send a request 18 00:01:00,250 --> 00:01:01,800 to delete that page. 19 00:01:01,800 --> 00:01:05,350 There's one more commonly used request type that Sinatra recognizes and 20 00:01:05,350 --> 00:01:07,095 that's delete request. 21 00:01:07,095 --> 00:01:08,285 But as with put requests, 22 00:01:08,285 --> 00:01:12,515 there's no way to send a delete requests from plain HTML links or forms. 23 00:01:12,515 --> 00:01:15,835 As with put the best way is to create an HTML form and 24 00:01:15,835 --> 00:01:19,525 use a hidden underscore method parameter with a value of delete. 25 00:01:19,525 --> 00:01:21,605 We won't add any text fields to this form, 26 00:01:21,605 --> 00:01:23,813 it's just going to consist of a single delete button. 27 00:01:23,813 --> 00:01:27,398 In the show.erb template, let's add a form element. 28 00:01:29,353 --> 00:01:31,570 We're going to place it within the div element so 29 00:01:31,570 --> 00:01:33,254 that it's easier to style it later. 30 00:01:35,913 --> 00:01:40,286 As before, the only requests type supported by HTML are get and post, so 31 00:01:40,286 --> 00:01:43,311 we'll set the form's method attribute to post. 32 00:01:44,830 --> 00:01:50,928 Then within the form, will add an input element with the type of hidden. 33 00:01:53,719 --> 00:02:00,060 It'll have a name of _method And a value of delete. 34 00:02:02,410 --> 00:02:06,440 That will cause Sinatra to delete the form submission as delete request. 35 00:02:06,440 --> 00:02:09,770 As with all the other requests types, delete request need a path to act on. 36 00:02:09,770 --> 00:02:13,170 We'll set that by adding an action attribute to the form tag. 37 00:02:14,520 --> 00:02:17,206 We'll give it the path of the wiki page we're trying to delete. 38 00:02:17,206 --> 00:02:22,163 That is a slash followed by the page title which will embed using an erb tag. 39 00:02:22,163 --> 00:02:26,148 As before, we can reuse the title instance variable which is already used elsewhere 40 00:02:26,148 --> 00:02:27,820 in the template. 41 00:02:27,820 --> 00:02:31,360 Lastly, we'll need the button that users will click to submit the requests. 42 00:02:31,360 --> 00:02:34,968 Will add an input element with a type of submit. 43 00:02:37,255 --> 00:02:40,406 The value attribute determines what the button will be labeled. 44 00:02:42,097 --> 00:02:46,310 So we'll give it a value of delete this page. 45 00:02:46,310 --> 00:02:50,040 Let's start up our server and run a preview. 46 00:02:51,620 --> 00:02:53,318 And now let's view a wiki page. 47 00:02:55,976 --> 00:02:58,450 We'll see a new Delete This Page button at the bottom. 48 00:03:00,420 --> 00:03:01,375 If we click that button, 49 00:03:01,375 --> 00:03:05,590 we'll see Sinatra doesn't know this ditty again because there's no matching route. 50 00:03:05,590 --> 00:03:06,680 At the bottom of the page, 51 00:03:06,680 --> 00:03:10,710 we'll see a recommendation to create a route using a method named delete. 52 00:03:10,710 --> 00:03:13,940 That means our form submission is being treated as a delete request. 53 00:03:15,320 --> 00:03:19,340 Now we just need to edit our wiki.rb file and add a delete route. 54 00:03:19,340 --> 00:03:20,820 Again, order doesn't matter. 55 00:03:20,820 --> 00:03:23,650 There's no way a delete request will accidentally be matched by 56 00:03:23,650 --> 00:03:26,010 our route's forget, post or put requests. 57 00:03:27,110 --> 00:03:31,040 Just as a method to set up a route for get requests is called GET and the method for 58 00:03:31,040 --> 00:03:34,820 post routes is called POST the method for delete routes is called Delete. 59 00:03:36,530 --> 00:03:37,960 So we'll add a call to that method now. 60 00:03:37,960 --> 00:03:42,510 And you probably know what comes next by now, the path to match. 61 00:03:42,510 --> 00:03:45,780 We set up our form to submit to a path of a slash followed by the current 62 00:03:45,780 --> 00:03:46,590 page title. 63 00:03:46,590 --> 00:03:50,666 So that's what will match here a slash followed by a URL parameter for the title. 64 00:03:52,926 --> 00:03:55,897 And of course we need a block to invoke when the routers match. 65 00:03:58,155 --> 00:04:01,450 In that block, what we'll have to do is call our delete_content method 66 00:04:01,450 --> 00:04:05,380 with the page title and it will delete the appropriate text file for us. 67 00:04:05,380 --> 00:04:09,116 So, we'll call delete_content and give it the title from the URL parameter. 68 00:04:12,628 --> 00:04:15,293 We'll also need to redirect the browser to another page, so 69 00:04:15,293 --> 00:04:16,820 we'll call the redirect method. 70 00:04:19,265 --> 00:04:20,910 We'll just redirect them to the root path. 71 00:04:22,210 --> 00:04:25,750 There's no need to call URI dot escape on the redirect path this time 72 00:04:25,750 --> 00:04:28,810 since there's no chance of it containing an invalid character. 73 00:04:29,920 --> 00:04:30,950 Save that. 74 00:04:30,950 --> 00:04:32,124 Let's restart our server. 75 00:04:36,029 --> 00:04:37,355 And go back to the home page. 76 00:04:37,355 --> 00:04:41,150 We'll create a new page to test our delete functionality out on. 77 00:04:42,945 --> 00:04:48,500 We'll use the title of John Doe and text of just some guy. 78 00:04:50,590 --> 00:04:51,770 Submit it. 79 00:04:51,770 --> 00:04:55,544 And if we go back to our workspace and refresh the file sidebar, 80 00:04:57,373 --> 00:05:00,910 We'll see John Doe.text here in the page's directory. 81 00:05:00,910 --> 00:05:02,380 If we go back to the new page and 82 00:05:02,380 --> 00:05:08,400 click delete, we'll be redirected back to the root path. 83 00:05:08,400 --> 00:05:13,360 And if we refresh our workspace sidebar again, we'll see if 84 00:05:13,360 --> 00:05:15,590 the John Doe.text file has been deleted.