在 rails ruby 重命名上传文件
Rename an upload file on ruby on rails
我是 rails 上 ruby 的新手,我想更改我上传的图片的名称
谢谢
def resize_image
resized_image = MiniMagick::Image.read(picture.image.download)
resized_image = resized_image.combine_options do |b|
b.resize '2760>'
b.quality '80'
end
v_filename = picture.image.filename
v_content_type = picture.image.content_type
picture.image.purge
picture.image.attach(io: File.open(resized_image.path), filename: v_filename, content_type: v_content_type)
end
picture.image.filename
属于 class ActiveStorage::Filename
所以 gsub
无法解决我们需要先使用 to_s
方法将其转换为字符串的问题那么我们可以用gsub
把space( )
换成下划线(_)
.
v_filename = v_filename.to_s.gsub(" ", "_")
我是 rails 上 ruby 的新手,我想更改我上传的图片的名称 谢谢
def resize_image
resized_image = MiniMagick::Image.read(picture.image.download)
resized_image = resized_image.combine_options do |b|
b.resize '2760>'
b.quality '80'
end
v_filename = picture.image.filename
v_content_type = picture.image.content_type
picture.image.purge
picture.image.attach(io: File.open(resized_image.path), filename: v_filename, content_type: v_content_type)
end
picture.image.filename
属于 class ActiveStorage::Filename
所以 gsub
无法解决我们需要先使用 to_s
方法将其转换为字符串的问题那么我们可以用gsub
把space( )
换成下划线(_)
.
v_filename = v_filename.to_s.gsub(" ", "_")