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 view pages that we've created for them. But a major feature of a wiki is the ability for users to create their own pages, and they can't do that yet. So let's fix that. Our next task is to set up a form so that users can create their own pages.
We've added another new method, save_content
, to the app. This one takes strings with a wiki page title and some content, and saves the content to a file:
def save_content(title, content)
File.open("pages/#{title}.txt", "w") do |file|
file.print(content)
end
end
We call File.open
with the name of a file we want to save our text to. The second argument is the mode to open the file in; a string of "w"
means we want to write to the file. If you provide a block to File.open
, it will pass a File
object to the block (that's the file
parameter), and automatically close the file when the block is done.
Within the block, we can call methods on the File
object to write data to the file. File
inherits a print
instance method from the IO
class; whatever string you pass to print
will be written to the file. (This is just like using print
to display data on your terminal's standard output; in fact standard output is treated just like a file.) You can read more about the print
method in the Ruby documentation.
-
0:00
[MUSIC]
-
0:04
Our users can view pages that we've created for them.
-
0:08
But a major feature of a wiki is the ability for
-
0:10
users to create their own pages.
-
0:12
And they can't do that yet.
-
0:14
So let's fix that.
-
0:15
Our next task is to set up a form for new pages.
-
0:18
First thing we're going to need is the ability to save text files with
-
0:21
the contents of new pages.
-
0:23
We already have the page content method that takes a page title and
-
0:26
uses it to load the contents of a text file in the pages directory.
-
0:31
I've added another method called save_contents to the app
-
0:33
which saves a file to the pages directory.
-
0:36
Again if you launch a workspace from this video's page,
-
0:38
you'll get an updated wiki .rb file with this method already set up for you.
-
0:43
The save_content method takes two strings as parameters.
-
0:46
The first string holds the page title and
-
0:48
the second holds the page content, save_content calls the core rubyfile.open
-
0:52
method to open a file in the pages directory.
-
0:55
It uses the title string as a filename and adds an extension of the .txt.
-
1:00
The string w in the second argument tells the method to open the file in write mode.
-
1:05
File.open takes a block and
-
1:07
passes an object representing the open file to the block as a parameter.
-
1:12
Within the block,
-
1:13
we can call the print method on the file object to write strings to the file.
-
1:16
So we simply call print and write the entire page contents to the file at once.
-
1:22
When the block finishes, control returns to the file.open method
-
1:25
which closes the file for us automatically.
-
1:28
Just like before,
-
1:29
I'll copy the save content method to a new file to show you how it works by itself.
-
1:34
I'll create a test.rb file again.
-
1:40
And paste this method in there.
-
1:42
Then I'll add a call to it which will create a page titled Ben Deitch.
-
1:46
So we'll call save_content.
-
1:49
First argument is the title, Ben Deitch.
-
1:55
Second argument is the page content which will just say,
-
1:59
Treehouse Android teacher.
-
2:06
Let's save this and then go to our console and try running it.
-
2:10
Ruby test.rb.
-
2:14
If we right click in the sidebar and choose refresh,
-
2:18
we'll see a new file in the pages directory called Ben Deitch.txt.
-
2:23
And that file will have the contents that we specified when we called
-
2:26
the save_content method.
-
2:27
We can also use save_contents top update existing files.
-
2:31
Let's change the method call to update our Nick Pettit.txt file.
-
2:35
We'll set the title in our method call to Nick Pettit.
-
2:40
And we'll change the content in the file to Treehouse teacher and awesome guy.
-
2:49
Save that and now, we can go to the console and run the script again.
-
2:53
Then if we open Nick Pettit.txt, we'll see our updated content.
-
2:58
As before,
-
2:58
we should delete the test.rb file when we're done to keep our project clean.
-
3:04
Again, understanding the save_content method isn't essential to understanding
-
3:08
Sinatra.
-
3:08
But there's more info in the teacher's notes if you want it.
-
3:11
So we've got a method that can take a wiki pages data and save it for us.
-
3:15
Now we're going to need an HTML form that lets users enter that data.
-
3:18
We'll look at that next.
You need to sign up for Treehouse in order to download course files.
Sign up