Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Start a free Courses 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.
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up