Search code examples
ruby-on-railsmongoidshrine

Shrine::Error: storage :cache isn't registered on FileUploader


Im using Mongoid 6.0.3, ruby 2.3.1 and rails 5.2

I created a file_uploader class for handling my uploads using shrine.

/app/uploaders/file_uploader.rb

class FileUploader < Shrine
    Attacher.validate do
        validate_max_size 5.megabytes, message: 'is too large (max is 5 MB)'
        validate_mime_type_inclusion %w(application/pdf image/png 
application/plain text/plain text/plain application/excel application/x- 
excel image/jpeg)
    end
end

config/initializers/Shrine.rb

require "shrine"
require "shrine/storage/file_system"
Shrine.storages = {
        cache: Shrine::Storage::FileSystem.new("public", prefix: 
"uploads/cache"), # temporary
        store: Shrine::Storage::FileSystem.new("public", prefix: "uploads"), #     permanent
}
Shrine.plugin :mongoid
Shrine.plugin :validation_helpers

But i get error when saving data,

In my Model it is called as

include FileUploader::Attachment.new(:file)
field :file_data

Solution

  • Shrine uses :file as default file type analyzer. After changing it to :mime_types in shrine initializer, it worked.