0

I need to unzip a zipped file containing log files and save them individually as different entries. I used "carrierwave" for uploading my files.

I found a method on the web for unzipping zipped files and I placed it in my ApplicationsController as a helper method so it can be available to different controllers. However, every time I try to unzip an uploaded zipped file nothing happens. I'm not sure what I'm doing wrong. I have already installed the gems "rubyzip" and "zip".

Here is my application_controller.erb:

class ApplicationController < ActionController::Base

    helper_method :airwave

    require 'rubygems'
    require 'zip'

    def airwave (file, destination)
    Zip::File.open(file) do |zip_file|
        zip_file.each do |f|
        f_path = File.join(destination, f.name)
             FileUtils.mkdir_p(File.dirname(f_path))
        f.extract(f_path) 
            end
         end
    end
end

Here is my index.html.erb:

<tbody>   
  <% @imports.each do |import| %>   

     <tr>   
        <td><h5><%= import.id %></h5></td>
        <td><h5><%= import.name %></h5></td>   
        <td><h5><%= link_to "Download", import.attachment_url %></h5></td>
        <td><h5><%= link_to "Unzip", method: :airwave%></h5></td>
        <td><h5><%= link_to "Delete",  import, method: :delete, confirm: "Are you sure you want to delete #{import.name}?" %></h5></td>   
     </tr>   

  <% end %>   
</tbody>
C.N.N.
  • 91
  • 8
  • you need to create new route and action in controller. in that action you need to pass file , then after you need to call airwave method of application controller. you can't directly call app controller method . – Vishal Jan 28 '19 at 08:33
  • Please also pass file and desintion path for method, destination path means where you want to unzip it – Vishal Jan 28 '19 at 08:34
  • @Vishal can you please reply with an example? I am sorry but I am just starting with ruby and rails so I am not yet that familiar with what you want me to do :( – C.N.N. Jan 28 '19 at 08:43

0 Answers0