Ruby on rails 保存我的ActiveRecord模型会删除我的CarrierWave图像版本
每当我使用装载的上传程序保存模型时,每当我保存它时,版本都会被删除(并且在正常上传图像时,版本似乎甚至没有创建)。请注意,直到几天前它还可以正常工作(我没有注意到确切的时间,尽管它可能是通过移动到Rails 3.2.11发生的。我之前是在3.2.8) 我的模型定义如下:Ruby on rails 保存我的ActiveRecord模型会删除我的CarrierWave图像版本,ruby-on-rails,carrierwave,rails-activerecord,Ruby On Rails,Carrierwave,Rails Activerecord,每当我使用装载的上传程序保存模型时,每当我保存它时,版本都会被删除(并且在正常上传图像时,版本似乎甚至没有创建)。请注意,直到几天前它还可以正常工作(我没有注意到确切的时间,尽管它可能是通过移动到Rails 3.2.11发生的。我之前是在3.2.8) 我的模型定义如下: class Profile < ActiveRecord::Base attr_accessible :profile_picture mount_uploader :profile_picture, Profil
class Profile < ActiveRecord::Base
attr_accessible :profile_picture
mount_uploader :profile_picture, ProfilePictureUploader
...
end
最后是我的BaseUploader
:
class ProfilePictureUploader < BaseUploader
process :resize_to_fill => [248, 248]
version :tiny do
process :resize_to_fill => [34, 34]
def full_filename(for_file = model.photo.file)
"#{model.to_s.parameterize}_tiny.jpg"
end
end
def extension_white_list
%w(jpg jpeg gif png)
end
# Override the filename of the uploaded files:
# Avoid using model.id or version_name here, see uploader/store.rb for details.
def filename
# John Snow -> john-snow-20120913162935.jpg
# ACME inc. -> acme-inc-20120913162935.png
"#{model.to_s.parameterize}-#{Time.now.strftime("%Y%m%d%H%M%S")}#{File.extname(original_filename)}" if original_filename
end
end
class BaseUploader < CarrierWave::Uploader::Base
include CarrierWave::RMagick
# Include the Sprockets helpers for Rails 3.1+ asset pipeline compatibility:
include Sprockets::Helpers::RailsHelper
include Sprockets::Helpers::IsolatedHelper
# Override the directory where uploaded files will be stored.
def store_dir
"system/uploads/#{model.class.to_s.underscore}/#{model.id}"
end
# Override the filename of the uploaded files:
def filename
"#{mounted_as}#{File.extname(original_filename)}" if original_filename
end
end
class BaseUploader
我在开发中使用文件存储,在生产中使用fog存储,但这两种环境中都会出现问题,所以我们假设它使用的是文件存储
以下是我的典型场景:
- 我将图像上传到配置文件,只创建标准图像(例如:
)mbillard-20130122102833.jpg
- 我调用
在配置文件图片上,创建了标准图像和微型版本(由于我的特殊命名方案,重新创建了标准,我同意)重新创建\u版本
- 保存模型后,文件夹(
)中的所有内容都会被删除,除了文件名为system/uploads/profile/1/
属性的图像profile\u picture
(我也同时打开了一个窗口)我终于找到了答案。基本上,
mount.rb
中的remove_previous_#{column}
方法似乎删除了我新创建的版本,因为它们与以前的版本同名
model.recreate_versions! # recreates versions but leaves the previous files there
model.save # removes the previous files
我修改了我的小版本文件名以包含时间戳(这样它就不会与以前的小版本具有相同的文件名):
我简化了filename
逻辑,以简单地定义带有时间戳的文件名:
# Override the filename of the uploaded files:
# Avoid using model.id or version_name here, see uploader/store.rb for details.
def filename
# John Snow -> john-snow-20120913162935.jpg
# ACME inc. -> acme-inc-20120913162935.png
"#{model.to_s.parameterize}-#{timestamp}#{File.extname(original_filename)}" if original_filename.present?
end
“我的时间戳”函数从文件名中提取时间戳,或在重新创建版本或上载新文件时生成新的时间戳(当存在原始文件名时):
请注意,前一个时间戳可以存储在我的模型的属性中,但这对我适用
现在唯一让我烦恼的是,我必须保存我的模型,以便在重新创建版本时删除以前的文件
model.recreate_versions! # recreates versions but leaves the previous files there
model.save # removes the previous files
你可以在会议上看到我的整个思考过程
model.recreate_versions! # recreates versions but leaves the previous files there
model.save # removes the previous files