为了节省云中的空间,您将如何使用activestorage对图像预上传进行调整和压缩?
发布于 2018-06-14 14:23:16
我在本地存储的开发中测试了下面的代码,它可以工作,但是无论如何,我会给出一些问题,我将在下面解释。
在创建时,这似乎很好,即使我认为应该有一个更干净的方法来做到这一点。
class User < ApplicationRecord
has_one_attached :avatar
before_save :resize_avatar_image
def resize_avatar_image
filename = avatar.filename.to_s
puts attachment_path = "#{Dir.tmpdir}/#{avatar.filename}"
File.open(attachment_path, 'wb') do |file|
file.write(avatar.download)
file.close
end
image = MiniMagick::Image.open(attachment_path)
# if image.width ...
image.resize "40x40"
image.write attachment_path
avatar.attach(io: File.open(attachment_path), filename: filename, content_type: "image/jpg")
end
end我遇到的问题有人可以克服
[ActiveJob] [ActiveStorage::PurgeJob] [d6a930ee-32cd-45a7-bfb5-72929d79f9bb] Error performing ActiveStorage::PurgeJob (Job ID: d6a930ee-32cd-45a7-bfb5-72929d79f9bb) from Async(default) in 0.33ms: ArgumentError (wrong number of arguments (given 0, expected 1));我找不到解决办法。检查是否删除了旧的blob。.attach方法有关;*.jpg和*.pnghttps://stackoverflow.com/questions/50844197
复制相似问题