重命名模型和迁移数据库时,如何重命名用于载波上传的文件夹?

How to rename a folder used for uploads in carrierwave when renaming models and migrating the db?

我正在尝试重命名模型和相应的表,结果很好。上传文件夹呢?使用类似的东西将上传器文件夹绑定到模型 class 名称。现在 class 名称已更改。

class CsvImportUploaderUploader < CarrierWave::Uploader::Base
  permissions 0755

  storage :file

  def store_dir
    "uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
  end
end

您可以继续使用相同的型号名称。 如果您有一个 User 模型,将从以下位置转换:

User => user

model.class.to_s.underscore 只是 returns class 将自己命名为带下划线的字符串,在此示例中为 "user"。

如果您想保留所有内容,只需照字面写即可:

"uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
"uploads/user/#{mounted_as}/#{model.id}"

如果您不在意名称,这些东西只会存储在不同的文件夹中。 如果您有一个 User 模型并将其更改为 Customer 模型,它会像这样更改:

"uploads/user/#{mounted_as}/#{model.id}"
"uploads/customer/#{mounted_as}/#{model.id}"

如您所见,它们只会存储在不同的文件夹中。您的所有链接应该仍然有效。