I'm using MiniMagick to resize my image. My method looks something like that:
def resize
mini_img = MiniMagick::Image.new(img.tempfile.path)
mini_img.combine_options do |c|
c.resize '50x50^'
c.gravity 'center'
c.extent '50x50'
end
mini_img
end
Resize works, but the problem is when I try save mini_img to Active Storage, because I get error Could not find or build blob: expected attachable, got #<MiniMagick::Image. Can I somehow convert MiniMagick::Image (mini_img) to normal image and save it into Active Storage?
Yes, you can. Currently you are trying to save an instance of
MiniMagick::Imageto ActiveStorage, and that's why you receive that error. Instead you should attach the :io directly to ActiveStorage.Using your example, if you wanted to attach
mini_imgto a hypotheticalUser, that's how you would do it:In this example I am calling
to_blobonmini_img, which is an instance ofMiniMagick::Image, and passing it as argument toStringIO#open. Make sure to include the:filenameoption when attaching to ActiveStorage this way.EXTRA
Since you already have the
content_typewhen usingMiniMagickyou might want to provide it to ActiveStorage directly.