class ImageUploader < CarrierWave::Uploader::Base
# Include RMagick or MiniMagick support:
include CarrierWave::RMagick
# include CarrierWave::MiniMagick
# Choose what kind of storage to use for this uploader:
storage :file
# storage :fog
# Override the directory where uploaded files will be stored.
# This is a sensible default for uploaders that are meant to be mounted:
def store_dir
"uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
end
def default_url
ActionController::Base.helpers.asset_path("fallback/" + [main, "default.png"].compact.join('_'))
end
# Create different versions of your uploaded files:
version :thumb do
process :resize_to_fit => [50, 50]
end
version :main do
process :auto_orient
process :resize_to_fit => [300, 360]
end
def auto_orient
manipulate! do |img|
img = img.auto_orient
end
end
end
我已将默认图片上传到app/assets/images/fallback/main/default.png。
这个default_url遵循了carrier wave gem描述的标准资产管道。它没有给我任何错误,但在视图上仅显示一个损坏的图像图标,而没有默认图片。然而,如果有有效的上传,它会正确显示上传,所以我知道:thumb和:main进程正在工作。唯一失效的是default_url,我不知道为什么。顺便说一下,我使用的是rails 4.有什么想法/建议吗?
谢谢!
我在视图中的代码会显示图片,如果上传成功则会正常显示,否则会显示损坏的默认链接。
<div class="img-container">
<%= image_tag @user.image_url(:main).to_s %>
</div>