2

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
ashusvirus
  • 412
  • 7
  • 22
  • The problem seems to be your `uploader` file is being loaded before `initializer`, hence it's not able to find that method. Can you try adding `require_relative './initializers/shrine'` below this line `Bundler.require(*Rails.groups)`. The [source](https://github.com/shrinerb/shrine/issues/155#issuecomment-292628097). You can have a look at [this](https://stackoverflow.com/questions/43263779/ruby-rails-shrineerror-storage-cache-isnt-registered-on-pdfuploader) – Kedarnag Mukanahallipatna Sep 11 '18 at 07:09
  • i restarted the application, sometimes it occurs sometimes it gives me an error of `"#` – ashusvirus Sep 11 '18 at 07:11
  • solved. Shrine uses :file as default type analyzer. i changed it to mime_types – ashusvirus Sep 11 '18 at 07:36

2 Answers2

3

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

ashusvirus
  • 412
  • 7
  • 22
0

I had to do the following to fix it:

As suggested by @ashusvirus

model.rb

old code

include ImageUploader::Attachment.new(:file)

new code

include ImageUploader::Attachment.new(:mime_types)

Additionally, I was using ActiveRecord instead of Sequel or Mongoid, so I had to change the following too:

config/shrine.rb

old code

Shrine.plugin :sequel

new code

Shrine.plugin :activerecord
harshainfo
  • 504
  • 7
  • 11