利用 Carrierwave 上传的图片在 production 环境中不显示


我项目中Uploader的代码如下:


 class PhotoUploader < CarrierWave::Uploader::Base

  include CarrierWave::MiniMagick
  process :resize_to_fit => [nil, 600]

  version :thumb do
    process :resize_to_fill => [150,150]
  end

  # Choose what kind of storage to use for this uploader:
  storage :file

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

  def cache_dir
    "#{Rails.root}/tmp/uploads"
  end

  def extension_white_list
    %w(jpg jpeg gif png)
  end

  def filename
    if original_filename
      @name ||= Digest::MD5.hexdigest(current_path)
      "#{@name}.#{file.extension}"
    end
  end

end

production.rb 中,设置 config.serve_static_assets = false

利用Capistrano部署到Nginx + Passenger的生产环境中后,每次上传图片后会根据uploader的设置生成两份,就像这样:
4759d1dd8b07036cbdc6ae0738112ea3.png

其中,访问第一个图片可以正常显示,访问第二个(version :thumb)处理过的图片无法显示,报出:


 ActionController::RoutingError (No route matches [GET] "/uploads/picture/photo/49/thumb_6d9596c7449d3714eadb74b9c71beec2.jpg")

这样的错误,而实际上这里面的 thumb_6d9596c7449d3714eadb74b9c71beec2.jpg 是存在于该路径下的。

所以,这是哪里出了错?该怎么办?

ruby Carrierwave nginx ruby-on-rails

邪恶的酒精灯 10 years, 3 months ago

可以通过 photo.url(:thumb) 试试

6351153 answered 10 years, 3 months ago

Your Answer