我有一个项目在运行。 我有一些基本的Ruby-On-Rails问题:
现在,让我解释一下:
答案 0 :(得分:0)
从技术上讲,您可以将文件夹放在任何位置,并像下面一样访问.md文件(只要您有权读取该文件):
def show
markdown=Redcarpet::Markdown.new(Redcarpet::Render::HTML)
md_file = IO.read("#{Rails.root}\static\pages\blog\post.md")
@post = markdown.render(md_file)
end
并在视图
<%= @post.html_safe %>
根据'rubyzip'gem的文档,您需要这样的东西才能使您的目录可压缩:
class ZipFileGenerator
# Initialize with the directory to zip and the location of the output archive.
def initialize(inputDir, outputFile)
@inputDir = inputDir
@outputFile = outputFile
end
# Zip the input directory.
def write()
entries = Dir.entries(@inputDir); entries.delete("."); entries.delete("..")
io = Zip::File.open(@outputFile, Zip::File::CREATE);
writeEntries(entries, "", io)
io.close();
end
# A helper method to make the recursion work.
private
def writeEntries(entries, path, io)
entries.each { |e|
zipFilePath = path == "" ? e : File.join(path, e)
diskFilePath = File.join(@inputDir, zipFilePath)
puts "Deflating " + diskFilePath
if File.directory?(diskFilePath)
io.mkdir(zipFilePath)
subdir =Dir.entries(diskFilePath); subdir.delete("."); subdir.delete("..")
writeEntries(subdir, zipFilePath, io)
else
io.get_output_stream(zipFilePath) { |f| f.print(File.open(diskFilePath, "rb").read())}
end
}
end
end
并在控制器处执行zip操作:
directoryToZip = "/pages"
outputFile = "/public/out#{rand(6**length).to_s(6)}.zip"
zf = ZipFileGenerator.new(directoryToZip, outputFile)
zf.write()
然后您可以向您的用户提供outputFile
的链接,您可能还需要删除该zip文件。