我不认为我正确理解这一点,但我如何为用户提供默认图像?
def default_url
"/images/fallback/" + [version_name, "default.png"].compact.join('_')
end
我的images / fallback目录中有一个名为'default.png'的图像。我在网上看到人们也将版本名称更改为“微小”,但这对我来说似乎也不起作用。这是怎么回事?版本名称究竟是什么?
我试过像
这样的东西 def default_url
"/images/fallback/default.png"
end
但这也不起作用。我有什么误会? 谢谢!
编辑:
class ImageUploader < CarrierWave::Uploader::Base
# Include RMagick or MiniMagick support:
include CarrierWave::RMagick
storage :file
def store_dir
"uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
end
# Provide a default URL as a default if there hasn't been a file uploaded:
def default_url
???????
end
# Create different versions of your uploaded files:
version :thumb do
process :resize_to_fill => [80, 80]
end
end
class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :token_authenticatable, :encryptable, :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
# Setup accessible (or protected) attributes for your model
attr_accessible :name, :email, :password, :password_confirmation,
:remember_me, :image, :remote_image_url, :image_cache
mount_uploader :image, ImageUploader
end
答案 0 :(得分:11)
如果您正在使用资产管道,那么您不需要在文件路径中包含“images”,因为image_tag
将自动路由到assets/images
文件夹,然后在那里查找文件名。因此,default_url
方法中所需的路径是images文件夹之后的路径。在我的情况下,它只是default.png,因为我在images文件夹中没有子文件夹。
def default_url
'default.png'
end
答案 1 :(得分:5)
例如,你有这样的上传者:
class MyUploader < CarrierWave::Uploader::Base
version :thumb do
process resize_to_fill: [280, 280]
end
version :small_thumb, :from_version => :thumb do
process resize_to_fill: [20, 20]
end
def default_url
"/images/fallback/" + [version_name, "default.png"].compact.join('_')
end
end
/images/fallback/default.png
代表原始图片,small_thumb
版本路径为/images/fallback/small_thumb_default.png
,依此类推。
如果您想要处理后图像实际保存的路径,则需要store_dir
def store_dir
'public/my/upload/directory'
end