Head over to RubyZip and install their gem:

# Add to Gemfile then bundle install

gem 'rubyzip'

We now need to require that gem on our controller:

Now create a like to the method, I call mine “process_and_create_zip_file”

# routes.rb

post '/download-attachments', to: "downloads#process_and_create_zip_file", as: 'download_documents'

In your setup, you will need to pass some sort of param to locate the Model for your documents:



# while files are being download onto server 😉

<%= link_to 'download', download_documents_path(job_id: # You may want to use JavaScript here instead to show an animation# while files are being download onto server 😉@job .id), method: :post %> JS Method. I like axios: some_function() {

// show some animation

axios.post('/download-attachments, {job_id: 1})

.then('remove animation')

}

What ever works for you.

When you click the download link, files from s3, in my case, downloads and store into a temporary folder located at “tmp/archive_12” on my server, loops over all files in that folder to create a zip file called “archive_12”. For the downloaded file for the user, I now called it “Files_for_job_title.zip”. Once clicked and zip is created, I then delete both file and folder.

If you can help me improve this code, please do lend a hand. Thanks.