Carrierwave;多个上传者还是只有一个?

时间:2023-01-23 19:17:41

I have a post model and a podcast model. Both models have an attribute titled: image. I'm using one Carrierwave uploader (named ImageUploader) to handle both models. I have two questions before I go into production.

我有一个帖子模型和一个播客模型。两个模型都有一个标题为:image的属性。我正在使用一个Carrierwave上传器(名为ImageUploader)来处理这两个模型。在我投入生产之前,我有两个问题。

Dumb question:

愚蠢的问题:

Is it ok to use the same uploader for two different models when they both have the same attribute name for their file attachements? sorry if it seems obvious

当它们的文件附件具有相同的属性名称时,是否可以为两个不同的模型使用相同的上传器?抱歉,如果这看起来很明显

Main question:

主要问题:

I want to create three versions of each blog post image (thumb, large, sepia) and only 1 version of each podcast image (thumb).

我想创建每个博客文章图像的三个版本(拇指,大,棕褐色),每个播客图像(拇指)只有1个版本。

Do I need to use two uploaders now or can I namespace with the one that I'm already using?

我现在需要使用两个上传器吗?或者我可以使用我已经使用的那个命名空间?

Again it probably seems obvious. I could probably have written the second uploader in the time its taken me to ask these questions

这似乎很明显。我可能已经写了第二个上传者,它带我去问这些问题

1 个解决方案

#1


41  

You can use the same uploader on different models even if they have different attribute names. e.g.

您可以在不同的模型上使用相同的上传器,即使它们具有不同的属性名称。例如

class Post
  mount_uploader :image, ImageUploader
end

class Podcast
  mount_uploader :photo, ImageUploader
end

Whether or not you'd want to is a different matter. In your case, I'd create different uploaders for each model, because they have different requirements. You can always use subclasses if you want to keep your code dry:

你是否愿意是另一回事。在您的情况下,我会为每个模型创建不同的上传器,因为它们有不同的要求。如果要保持代码干燥,可以始终使用子类:

class ImageUploader < Carrierwave::Uploader::Base; end  # thumbnail
class PostImageUploader < ImageUploader; end  # thumbnail (from superclass), large & sepia
class PodcastImageUploader < ImageUploader; end # thumbnail (from superclass)

#1


41  

You can use the same uploader on different models even if they have different attribute names. e.g.

您可以在不同的模型上使用相同的上传器,即使它们具有不同的属性名称。例如

class Post
  mount_uploader :image, ImageUploader
end

class Podcast
  mount_uploader :photo, ImageUploader
end

Whether or not you'd want to is a different matter. In your case, I'd create different uploaders for each model, because they have different requirements. You can always use subclasses if you want to keep your code dry:

你是否愿意是另一回事。在您的情况下,我会为每个模型创建不同的上传器,因为它们有不同的要求。如果要保持代码干燥,可以始终使用子类:

class ImageUploader < Carrierwave::Uploader::Base; end  # thumbnail
class PostImageUploader < ImageUploader; end  # thumbnail (from superclass), large & sepia
class PodcastImageUploader < ImageUploader; end # thumbnail (from superclass)

相关文章