在 rails 种子文件中使用它来选择随机图像。
Room.all.each do |room|
count = rand(1..5)
count.times do
room.photos.create!(
image: File.new(Dir['app/assets/images/sampleimages/*.jpg'].sample))
end
end
但是在阅读了它从我的资产文件夹中删除该图像之后。这里可能是什么问题?上面的代码是假设这样做还是与shrine
(图像上传器)有关?
使用版本Rails 5.2.0
和shrine 2.10.1
.
我的满imageUploader.rb
class ImageUploader < Shrine
include ImageProcessing::MiniMagick
plugin :processing
plugin :determine_mime_type
plugin :remove_attachment
plugin :store_dimensions
plugin :validation_helpers
plugin :versions
plugin :pretty_location
plugin :delete_raw
Attacher.validate do
validate_max_size 5.megabytes, message: 'is too large (max is 5 MB)'
validate_mime_type_inclusion ['image/jpeg', 'image/png', 'image/gif']
end
def process(io, context)
case context[:phase]
when :store
original = io.download
pipeline = ImageProcessing::MiniMagick.source(original)
size_300 = pipeline.resize_to_fit!(300, 300)
size_150 = pipeline.resize_to_fill!(150, 150)
original.close!
{original: io, medium: size_300, thumb: size_150}
end
end
end