Validation Block Not Getting Called

My validation block does not seem to be getting called. Resizing and uploads work fine.

I have the following configured:

shrine.rb

Shrine.plugin :activerecord
Shrine.plugin :cached_attachment_data
Shrine.plugin :restore_cached_data
Shrine.plugin :validation
Shrine.plugin :validation_helpers
Shrine.plugin :store_dimensions
Shrine.plugin :remove_invalid

image_uploader.rb

class ImageUploader < Shrine
  Attacher.validate do
    validate_mime_types %w(image/jpg image/jpeg image/gif image/png)
    validate_max_size MAX_ATTACHMENT_SIZE
    validate_width 100..MAX_WIDTH
    validate_height 100..MAX_HEIGHT
  end
end

my_model.rb

class MyModel < ApplicationRecord
  include Shrine::Attachment(:image1)
end

Could you create a self-contained script that reproduces the problem? You can use a template from here.

Here is a sample project I made. There must be something very simple I’m doing wrong.

In your model, you’re using the base Shrine uploader that doesn’t have any validation blocks. You need to use the ImageUploader instead.

    # Change 
    include Shrine::Attachment(:image1)
    # to
    include ImageUploader::Attachment(:image1)
1 Like