我目前正在使用Carrierwave远程下载图像。这工作正常。我想弄清楚的是如何将文件名设置为除了从URL下载的内容之外的其他内容。
这是我的佣金任务:
@desk.desk_images.create(
:file_name_to_use => "testing",
:remote_image_url => photo.url(size)
)
在我的上传者群中: desk_image_uploader.rb
class DeskImageUploader < CarrierWave::Uploader::Base
include CarrierWave::MiniMagick
storage :fog
def store_dir
"uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
end
version :thumb do
process resize_to_fill: [200, 150]
process :quality => 90
def full_filename(for_file = model.logo.file)
parts = for_file.split('.')
extension = parts[-1]
name = parts[0...-1].join('.')
"#{model.file_name_to_use}_#{version_name}.#{extension}"
end
end
end
desk_image.rb
class DeskImage < ActiveRecord::Base
belongs_to :desk
mount_uploader :image, DeskImageUploader
attr_accessor :file_name_to_use
end
这不能正确存储在数据库中。 DeskImage.image存储的是orig文件名,而不是我通过“测试”的覆盖...
任何人都知道我如何自定义文件名Carrierwave在数据库中存储并使用?
谢谢
答案 0 :(得分:1)
好的,我已经删除了一些代码,以确保我们可以确定没有任何混淆因素。第一步是将filename
移出version
块。你能尝试一下这个:
class DeskImageUploader < CarrierWave::Uploader::Base
include CarrierWave::MiniMagick
storage :fog
def store_dir
"uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
end
version :thumb do
process resize_to_fill: [200, 150]
process :quality => 90
end
def filename
"test-#{model.id}.#{File.extname(super)}"
end
end