troubleshooting Question

paperclip upload zip file

Avatar of lusfernandos
lusfernandos asked on
Ruby
1 Comment1 Solution3423 ViewsLast Modified:
Hi, I am trying to upload a zip file, open the file which should contains image files.
My idea was to create an upload button for a file, unzip the file and get image by image, create a Photo which is the class which has has_attached_file.
I am able to upload the file and seems I am able to extract each image file, however I am having problems when creating the Photo which will be added to an event( event has_many photos).
Here is my function where I am trying to create photos.
  def create_photos_from_zip
    logger.debug "create_photos_from_zip -->"
    zip = params[:zip]
    logger.debug "unzipping file #{zip.path} class #{zip.class}"

    Zip::ZipFile.open(zip.path).each do |entry|
      # entry is a Zip::Entry
      filename = "#{Dir::tmpdir}/#{current_user.name}_#{entry.name}"
      logger.debug "will extract file to #{filename} to"
      entry.extract(filename)
      p = File.open(filename, 'r+')
      @event.photos << Photo.create(:photo=>p)
      #delete the temp file
      #FileUtils.remove_file(filename)
    end
    res = @event.save
    logger.debug "create_photos_from_zip <--"
    return res
  end

This is my create function
 def create
    if (params[:zip].present?)
      res = create_photos_from_zip
      render :nothing=>true
    else
      @photo = @event.photos.build(params[:photo])
      res = @photo.save
      respond_to do |format|
        if res
          flash[:notice] = 'Photo was successfully created.'
          format.html { redirect_to([@event,@photo]) }
          format.xml  { render :xml => @photo, :status => :created, :location => @photo }
        else
          format.html { render :action => "new" }
          format.xml  { render :xml => @photo.errors, :status => :unprocessable_entity }
        end
      end      
    end
  end
* I know I need to work more here, but I am stuck !!!

I am not getting any error, however the files are not attached to the event as well, I am not sure if I am unzipping at the client or server, I am developing in the same machine ...
Any Help would be very appreciated!
Please let me know If you need any other information.
Join the community to see this answer!
Join our exclusive community to see this answer & millions of others.
Unlock 1 Answer and 1 Comment.
Join the Community
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 1 Comment.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros