我认为即时通讯无法正确理解,但是如何为用户提供默认图片?
def default_url
"/images/fallback/" + [version_name, "default.png"].compact.join('_')
end
我的images/fallback目录中有一个名为“default.png”的图像。我在网上看到的人们也将version_name更改为“tiny”之类的东西,但这似乎也不适合我。这是怎么回事?版本名称究竟是什么?
我尝试过类似的东西
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
最佳答案
例如,您有这样的上传器:
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
关于ruby-on-rails - Rails carrierwave default_url不起作用,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/10100529/