You need to create a separate /data folder to store all the files.
You need to have a root to current folder file by calling this
file_path = File.expand_path("..", __FILE__)
This will create a file path from root until one level above current file, which is the folder that contains your file.
When reading text file, you need to specify
headers['Content-Type'] = 'text/plain'
File.read(file_path)
To edit an existing file or to create a new file, need to
File.write(file_path , "content here")
To delete a file,
File.delete(file_path)
To determine whether a file exist or not
File.exist?(file_path)
File.file?(file_path)
To list down all file within a certain directory. The Dir.glob will create an array list of the matching file. File.basename will cut off the last part of the file name.
Dir.glob(root + "/data/*").map do |path|
File.basename(path)
end
As you can see, most File class takes in the file_path as argument. So it is important to have that.
You need to have a root to current folder file by calling this
file_path = File.expand_path("..", __FILE__)
This will create a file path from root until one level above current file, which is the folder that contains your file.
When reading text file, you need to specify
headers['Content-Type'] = 'text/plain'
File.read(file_path)
To edit an existing file or to create a new file, need to
File.write(file_path , "content here")
To delete a file,
File.delete(file_path)
To determine whether a file exist or not
File.exist?(file_path)
File.file?(file_path)
To list down all file within a certain directory. The Dir.glob will create an array list of the matching file. File.basename will cut off the last part of the file name.
Dir.glob(root + "/data/*").map do |path|
File.basename(path)
end
As you can see, most File class takes in the file_path as argument. So it is important to have that.
Comments
Post a Comment