我試圖創建一個表,其中顯示一個zip文件中像這樣的內容:如何直接在ruby文件中使用循環創建haml表?
Name Size
asdf1.jpg 100KB
asdf2.jpg 200KB
asdf3.jpg 300KB
我的代碼是在這裏(實際上,我從ZipRuby的README複製它):
#myapp.rb
post 'checkfile/?' do
Zip::Archive.open('zip_file.zip') do |ar|
n = ar.num_files
n.times do |i|
entry_name = ar.get_name(i) # get entry name from archive
# open entry
ar.fopen(entry_name) do |f| # or ar.fopen(i) do |f|
$name = f.name # name of the file
$size = f.size # size of file (uncompressed)
$comp_size = f.comp_size # size of file (compressed)
content = f.read # read entry content
end
end
# Zip::Archive includes Enumerable
entry_names = ar.map do |f|
f.name
end
end
haml :checkresult
end
而我的哈姆碼:
-# checkresult.haml
%table
%thead
%tr
%th Name
%th Size
%tbody
%tr
-# I want to show files in zip here
對不起,英文不好,標題不好。 (使用Sinatra v1.4.6(使用Puma。))
你確定你使用的是'的Ruby on Rails'?你能確認你正在使用的Web框架嗎? –
使用Sinatra v1.4.6(與彪馬。)感謝您的意見! :) – Dogdriip