我正在尝试遍历图像文件目录并重命名它们。每次我运行此方法时,它都会重命名一个文件并删除其余文件。我还使用了 File.rename。
require 'fileutils'
def rename_image_files
@last_product = Product.find(:last).id + 1
Dir.foreach("app/assets/images/products/#{@last_product}").each do |item|
@count = 0
@count += 1
new_name = "#{@last_product.to_s << '_'}#{@count}"
@item = item
unless @item == '.' or @item == '..'
FileUtils.mv("app/assets/images/products/#{@last_product}/#{@item}", "app/assets/images/products/#{@last_product}/#{new_name}.png")
end
end