Search code examples
iosruby-on-railspaperclip

Paperclip throws NoHandlerError with base64 photo


When upgrading from paperclip 2 to paperclip 3 my image upload API brakes. This code used to work :

def decode_cover_image_data(cover_image_data)
    data = StringIO.new(Base64.decode64(cover_image_data))
    data.class.class_eval { attr_accessor :original_filename, :content_type }
    data.original_filename = "cover.png"
    data.content_type = "image/png"
    
    self.photo = data
end

It now raises a NoHandleError exception:

"Paperclip::AdapterRegistry::NoHandlerError (No handler found for "/9j/4AAQD/4Q[...]wooooGf/9k="

Did anybody encounter this problem?


Solution

  • I found where the problem was coming from, it's quite stupid...

    In my controller update method i had something like :

      def update
        ...
        if p[:photo]
          @user_ipad.decode_cover_image_data(p[:photo])
        end
    
        unless @user_ipad.update_attributes(p)
          render :json => {:errors => @user_ipad.errors}
        end
      end
    

    The issue is that i'm setting the user photo 2 times :

    • Once in @user_ipad.decode_cover_image_data(p[:photo])
    • Once in @user_ipad.update_attributes(p)

    The crash occured when saving the base64 string (still in the parameters) with update_attributes. Actually what surprise me is that this piece of code works with Paperclip 2.4.5 ...

    After upgrading to Paperclip 3.3.1 i solved my problem by replacing my update method with this :

      def update
        ...
        if p[:photo]
          p[:photo] = @user_ipad.decode_cover_image_data(p[:photo])
        end
    
        unless @user_ipad.update_attributes(p)
          render :json => {:errors => @user_ipad.errors}
        end
      end