1

I'm currently converting a very small Rails application to Sinatra. This Rails app was relying on ActiveRecord + Paperclip + Amazon S3 for picture storage, and I have troubles making it work in Sinatra (but I believe it would be the same for any kind of Rack-based application), with ActiveRecord + Paperclip + Amazon S3 as well.

Here's what I have so far:

Gemfile:

gem 'paperclip'
gem 'paperclip-rack', require: 'paperclip/rack'
gem 'aws-sdk'

Model:

class Photo < ActiveRecord::Base
  include Paperclip::Glue 
  has_attached_file :image,
                    :storage => :s3,
                    :s3_credentials => "config/s3.yml",
                    :bucket => 'mybucket',
                    :path => ':style/:photo_id.:extension',
                    :styles => {
                      :original => '1200x1200>',
                      :miniature => '80x80#',
                      :slideshow => 'x200'
                    }

end

View:

form method="post" action="/photos/add" enctype='multipart/form-data'
  input type="file" name="image"
  input#submit-button type="submit"

Route/Action:

  post '/photos/add' do
    photo = Photo.new
    photo.image = params[:image]
      #image[:tempfile]     = params[:image][:tempfile],
      #image[:filename]     = params[:image][:filename],
      #image[:content_type] = params[:image][:type],
      #image[:size]         = params[:image][:tempfile].size
    photo.save

    redirect "/"
  end

And the error I'm getting when trying to upload something :

NoMethodError at /admin/photos/add
undefined method `descendants' for Paperclip::Validators::AttachmentFileNameValidator:Class
file: attachment.rb location: each line: 393

I've tried to play with the :image param in my route (and manually assign each value to the field that was created by paperclip, see the commented lines) but it didn't seem to work any better. Any idea guys ? I'm stuck and have no clue where to start to get this to work.

Note: I've removed all validations and stuff so I even don't understand the error message I'm getting.

Pierre-Adrien
  • 2,836
  • 29
  • 30

2 Answers2

1

I (think) I was able to monkey patch the #descendants method using this SO answer:

Look up all descendants of a class in Ruby

So I monkey-patched Paperclip in one of my initializers, which seemed okay to me since I don't care about validating the content type of the attachment:

module Paperclip
  module Validators
    class AttachmentFileNameValidator
      def self.descendants
        ObjectSpace.each_object(Class).select { |klass| klass < self }
      end
    end
    class AttachmentContentTypeValidator
      def self.descendants
        ObjectSpace.each_object(Class).select { |klass| klass < self }
      end
    end
    class AttachmentFileTypeIgnoranceValidator
      def self.descendants
        ObjectSpace.each_object(Class).select { |klass| klass < self }
      end
    end
  end
end

I'm using Paperclip 4.2 and Sinatra 1.4.5

Community
  • 1
  • 1
mzemel
  • 100
  • 7
0

There is no official support for non-Rails apps using Paperclip. See a discussion on it here

You'll notice if you look in Paperclip::Validators::AttachmentFileNameValidator:Class you'll see:

class AttachmentFileNameValidator < ActiveModel::EachValidator
  def initialize(options)
    options[:allow_nil] = true unless options.has_key?(:allow_nil)
    super
  end

ActiveModel is bundled w/ rails, so I'm guessing it's blowing up on the inheritance in here. Either way, I'd move over to an uploader friendly to sinatra like carrierwave

Anthony
  • 15,435
  • 4
  • 39
  • 69
  • Yeah you're right, I've seen this discussion about Paperclip for non-Rails app. It seemed to me that t-k's gem, 'paperclip-rack', was dealing with this, and that it should be working as intended. As for carrierwave, do you have any experience about the compatibility between Paperclip attachments and migration to Carrierwave ? That's my main concern. – Pierre-Adrien Sep 26 '14 at 06:44
  • I've never personally done the migration but there appears to be a lot on the web regarding it [for instance here](http://bessey.io/blog/2013/04/07/migrating-from-paperclip-to-carrierwave/) – Anthony Sep 26 '14 at 17:42