1 00:00:00,000 --> 00:00:04,757 [MUSIC] 2 00:00:04,757 --> 00:00:08,040 Our users can view pages that we've created for them. 3 00:00:08,040 --> 00:00:10,360 But a major feature of a wiki is the ability for 4 00:00:10,360 --> 00:00:12,240 users to create their own pages. 5 00:00:12,240 --> 00:00:14,140 And they can't do that yet. 6 00:00:14,140 --> 00:00:15,350 So let's fix that. 7 00:00:15,350 --> 00:00:18,560 Our next task is to set up a form for new pages. 8 00:00:18,560 --> 00:00:21,720 First thing we're going to need is the ability to save text files with 9 00:00:21,720 --> 00:00:23,600 the contents of new pages. 10 00:00:23,600 --> 00:00:26,780 We already have the page content method that takes a page title and 11 00:00:26,780 --> 00:00:31,010 uses it to load the contents of a text file in the pages directory. 12 00:00:31,010 --> 00:00:33,660 I've added another method called save_contents to the app 13 00:00:33,660 --> 00:00:36,340 which saves a file to the pages directory. 14 00:00:36,340 --> 00:00:38,750 Again if you launch a workspace from this video's page, 15 00:00:38,750 --> 00:00:43,160 you'll get an updated wiki .rb file with this method already set up for you. 16 00:00:43,160 --> 00:00:46,350 The save_content method takes two strings as parameters. 17 00:00:46,350 --> 00:00:48,050 The first string holds the page title and 18 00:00:48,050 --> 00:00:52,930 the second holds the page content, save_content calls the core rubyfile.open 19 00:00:52,930 --> 00:00:55,910 method to open a file in the pages directory. 20 00:00:55,910 --> 00:01:00,760 It uses the title string as a filename and adds an extension of the .txt. 21 00:01:00,760 --> 00:01:05,307 The string w in the second argument tells the method to open the file in write mode. 22 00:01:05,307 --> 00:01:07,850 File.open takes a block and 23 00:01:07,850 --> 00:01:11,270 passes an object representing the open file to the block as a parameter. 24 00:01:12,500 --> 00:01:13,240 Within the block, 25 00:01:13,240 --> 00:01:16,930 we can call the print method on the file object to write strings to the file. 26 00:01:16,930 --> 00:01:20,930 So we simply call print and write the entire page contents to the file at once. 27 00:01:22,520 --> 00:01:25,970 When the block finishes, control returns to the file.open method 28 00:01:25,970 --> 00:01:28,600 which closes the file for us automatically. 29 00:01:28,600 --> 00:01:29,385 Just like before, 30 00:01:29,385 --> 00:01:33,260 I'll copy the save content method to a new file to show you how it works by itself. 31 00:01:34,480 --> 00:01:36,612 I'll create a test.rb file again. 32 00:01:40,982 --> 00:01:42,900 And paste this method in there. 33 00:01:42,900 --> 00:01:46,330 Then I'll add a call to it which will create a page titled Ben Deitch. 34 00:01:46,330 --> 00:01:48,370 So we'll call save_content. 35 00:01:49,730 --> 00:01:52,120 First argument is the title, Ben Deitch. 36 00:01:55,910 --> 00:01:59,330 Second argument is the page content which will just say, 37 00:01:59,330 --> 00:02:03,580 Treehouse Android teacher. 38 00:02:06,615 --> 00:02:10,287 Let's save this and then go to our console and try running it. 39 00:02:10,287 --> 00:02:14,572 Ruby test.rb. 40 00:02:14,572 --> 00:02:18,356 If we right click in the sidebar and choose refresh, 41 00:02:18,356 --> 00:02:23,350 we'll see a new file in the pages directory called Ben Deitch.txt. 42 00:02:23,350 --> 00:02:26,143 And that file will have the contents that we specified when we called 43 00:02:26,143 --> 00:02:27,900 the save_content method. 44 00:02:27,900 --> 00:02:31,040 We can also use save_contents top update existing files. 45 00:02:31,040 --> 00:02:33,890 Let's change the method call to update our Nick Pettit.txt file. 46 00:02:35,020 --> 00:02:40,850 We'll set the title in our method call to Nick Pettit. 47 00:02:40,850 --> 00:02:46,720 And we'll change the content in the file to Treehouse teacher and awesome guy. 48 00:02:49,190 --> 00:02:51,900 Save that and now, we can go to the console and run the script again. 49 00:02:53,270 --> 00:02:58,000 Then if we open Nick Pettit.txt, we'll see our updated content. 50 00:02:58,000 --> 00:02:58,580 As before, 51 00:02:58,580 --> 00:03:02,200 we should delete the test.rb file when we're done to keep our project clean. 52 00:03:04,430 --> 00:03:08,010 Again, understanding the save_content method isn't essential to understanding 53 00:03:08,010 --> 00:03:08,830 Sinatra. 54 00:03:08,830 --> 00:03:11,850 But there's more info in the teacher's notes if you want it. 55 00:03:11,850 --> 00:03:15,360 So we've got a method that can take a wiki pages data and save it for us. 56 00:03:15,360 --> 00:03:18,910 Now we're going to need an HTML form that lets users enter that data. 57 00:03:18,910 --> 00:03:19,680 We'll look at that next.