I have followed the CarrierWave documentation for multi-uploaders and you can upload multiple files as expected, however if the user uploads files a second time the original files get moved from file.png to file(2).png, but the database record does not get updated, thereby preventing the image from being loaded.
How can I either prevent the existing files from being moved or update my model with the updated filename?
class ProfilePhotoUploader < CarrierWave::Uploader::Base
include CarrierWave::MiniMagick
# 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}/#{model.id}/#{mounted_as}"
end
# Create different versions of your uploaded files:
version :thumb do
process resize_to_fit: [250, 250]
end
# Add an allowlist of extensions which are allowed to be uploaded.
# For images you might use something like this:
def extension_allowlist
%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
original_filename if original_filename.present?
end
end
Controller:
profile_params = params.require(:profile).permit([portraits: []])
@profile.portraits += profile_params[:portraits] if profile_params[:portraits]
Model:
Class Profile < ApplicationRecord
...
mount_uploaders :portraits, ProfilePhotoUploader
...
end
One approach would be to override the
filenamemethod in your uploader class to make sure the original filename remains unchanged even if the same file is uploaded again.(you have a similar approach in
carrierwaveuploader/carrierwaveissue 961 for a different problem)And, whenever a file is uploaded, you would update the model with the new filename in your controller.
Your code would be:
The OP Kevin adds in the comments: