Ruby on rails 保存前调整Rails图像大小

Ruby on rails 保存前调整Rails图像大小,ruby-on-rails,ruby-on-rails-6,minimagick,Ruby On Rails,Ruby On Rails 6,Minimagick,我想在保存图像之前调整其大小。目前,这是在控制器中完成的: shrink = MiniMagick::Image.new(params[:user][:photo].tempfile.path) shrink.resize "500x500" 这很管用,但感觉不是很瘦。我更愿意在模型中使用这个,我假设它看起来像这样: before_save :resize_image def resize_image shrink = MiniMagick::Image.new(

我想在保存图像之前调整其大小。目前,这是在控制器中完成的:

shrink = MiniMagick::Image.new(params[:user][:photo].tempfile.path)
shrink.resize "500x500"
这很管用,但感觉不是很瘦。我更愿意在模型中使用这个,我假设它看起来像这样:

    before_save :resize_image

    def resize_image
      shrink = MiniMagick::Image.new(self.photo)
      shrink.resize "500x500"
    end
这,;然而,耶尔德指出了错误:

`magick mogrify -resize 500x500 #<ActiveStorage::Attached::One:0x00007ffc866418d0>` failed with error:
mogrify: unable to open image '#<ActiveStorage::Attached::One:0x00007ffc866418d0>': No such file or directory @ error/blob.c/OpenBlob/3496.
mogrify: no decode delegate for this image format `' @ error/constitute.c/ReadImage/562.
 excluded from capture: Not configured to send/capture in environment 'development'
`magick mogrify-调整大小500x500#`失败,出现错误:
mogrify:无法打开图像“#”:没有这样的文件或目录@error/blob.c/OpenBlob/3496。
mogrify:此图像格式“%”没有解码委托@error/compose.c/ReadImage/562。
从捕获中排除:未配置为在环境“开发”中发送/捕获

我尝试过各种变体,例如
self.photo.path
,但到目前为止,我还找不到一种适用于活动存储对象的方法。我错过了什么?我假设这样的东西首先属于模型而不是控制器,对吗?

不是答案:为什么要使用Minimagik,ActiveStorage内置了mini_magick。我没有意识到这一点,在不使用Minimagik的情况下,如何在使用ActiveStorage的情况下实现上传时的图像大小调整。另外,你所说的“干净”是什么意思?签出图像变体选项:据我所知,这只是创建原始保存图像的变体,有没有办法在保存之前收缩图像?没有答案:为什么要使用MIniMagick,ActiveStorage内置了mini_magick。我没有意识到这一点,在不使用Minimagik的情况下,如何在使用ActiveStorage的情况下实现上传时的图像大小调整。另外,你所说的“清理”是什么意思?签出图像变体选项:从我所能看出,这只是创建原始保存图像的变体,有没有办法在保存之前收缩图像?