载波多个上传者还是一个? [英] Carrierwave; multiple uploaders or just one?

查看:53
本文介绍了载波多个上传者还是一个?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

哑巴问题

两个可以使用相同的上传器它们的文件附件具有相同的属性名称的不同模型? 抱歉,似乎很明显

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

主要问题

我想为每个博客帖子图像(缩略图,大字体,棕褐色)创建三个版本,而每个播客图像(缩略图)仅创建一个版本。

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

我需要吗现在使用两个上传器,还是可以为已经使用的一个上传器命名空间?

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

推荐答案

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

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)

这篇关于载波多个上传者还是一个?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆