Carrierwave:有时无法找到创建的版本

时间:2015-02-25 07:41:19

标签: ruby carrierwave minimagick

我的Carrierwave上传器文件是这样的,我检查了新版本是否正确创建。问题是,有时视图部分找不到创建的版本。

 class ImageUploader < CarrierWave::Uploader::Base
  include CarrierWave::MiniMagick
  storage :fog

  def store_dir
   "uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
  end

  def store_dimensions
     if file && model
      width, height = ::MiniMagick::Image.open(file.file)[:dimensions]
        if width>700
              finalHeight=((700*height)/width)
              self.class.version :best_fit do
                process :resize_to_fill => [700,finalHeight]
              end
        else
              self.class.version :best_fit do
                process :resize_to_fill => [width,height]
              end
        end
        end
  end

#run the store_dimensions methods
  process :store_dimensions

end

视图文件如下:

<%= image_tag pp.image_url(:best_fit), id: "plot-image-#{pp.id}" %>

上传后,有时会显示页面,有时会显示以下信息:

ActionView::Template::Error (Version best_fit doesn't exist!):

1 个答案:

答案 0 :(得分:0)

我们需要在方法之后提及以下几行。显然store_dimensions仅在上传图像时有效。因此,在该会话到期后,将找不到版本代码。因此,以下两行代码是必要的。

version :best do
end

因此,代码如下所示。      进程:store_dimensions

 def store_dimensions
     if file && model
      width, height = ::MiniMagick::Image.open(file.file)[:dimensions]
        if width>700
              finalHeight=((700*height)/width)
              self.class.version :best do
                process :resize_to_fill => [700,finalHeight]
              end
        else
              self.class.version :best do
                process :resize_to_fill => [width,height]
              end
        end
      end
end


 #this would load compressed version during loading
 #if it is ommited, after restarting server, images won't be loaded
 version :best do
 end