2015-10-16 117 views
1

您好我已經創建了csv file,我想在我的rails應用程序中下載該文件的.zip-file。請幫幫我。我無法做到這一點如何將該文件添加到zip文件並下載該zip文件。如何在rails中渲染該文件。Ruby On Rails:創建csv文件並下載爲.zip-archive

以下是我已經嘗試了一段代碼,

 # method to create csv 
    def to_csv 
     CSV.generate do |csv| 
     #build csv format of document 
     end 
    end 

    # controller action to download zip 
    def zip_download 
     zip = Zip::File.open('document.zip', Zip::File::CREATE) do |zipfile| 
       @documents.each do |document| 
       # document.to_csv how to add this file .zip 
       end 
      end 
     send_data zip, filename: "abc.zip", type: 'application/zip' 
    end 

回答