我正在努力将应用程序升级到 Rails 3,但 Attachment_fu 已损坏,因此我正在转向 Carrierwave。是否有一个系统流程可以让我从attachment_fu升级到carrierwave?或者它的教程?现在,我更感兴趣的是让数据库端的一切都正确。我正在使用 Attachment_fu 和 Carrierwave 的文件系统存储选项。
我从 UploaderFu >http://ruby.smapse.com/2011/03/migrate-attachmentfu-to-rierwave.html 告诉 Carrierwave 使用与以下相同的目录和文件名Attachment_fu。但这不是全部答案,只是其中的一部分。
例如,在数据库中,我有一个 UserImage
模型,其中 :filename
、:content_type
、:size
、:width
、:height
和 :user_id
属性。我在我的模型中添加了一个 :user_avatar
列,以及以下内容
attr_accessible :user_avatar
mount_uploader :user_avatar, UserAvatarUploader
:user_avatar
中到底存储了什么。只是文件名吗?或者其他什么?我是否只需要编写一个迁移来将 :filename
中的数据(存储方式为 "hello_world.png"
)移动到 :user_avatar
?如果是这种情况,我应该只使用原始的 :filename
而不是创建 :user_avatar
列,对吧?
I'm working on upgrading an app to Rails 3, and attachment_fu is broken so I'm moving to carrierwave. Is there a systematic process that I can go through to upgrade from attachment_fu to carrierwave? Or a tutorial for it? Right now, I'm more interested in getting everything on the database end right. I'm using the filesystem store option for attachment_fu and carrierwave.
I've found a module, UploaderFu
from http://ruby.simapse.com/2011/03/migrate-attachmentfu-to-carrierwave.html that tells carrierwave to use the same directories and filenames as attachment_fu. But it's not the entire answer, just part of it.
For example, in the db, I have a UserImage
model, with :filename
, :content_type
, :size
, :width
, :height
, and :user_id
attributes. I added a :user_avatar
column, and the following to my model
attr_accessible :user_avatar
mount_uploader :user_avatar, UserAvatarUploader
What exactly gets stored in :user_avatar
. Is it just the filename? or something else? Do I just need to write a migration to move the data in :filename
(stored like "hello_world.png"
) to :user_avatar
? If that's the case I should just use the original :filename
instead of creating a :user_avatar
column, right?
发布评论
评论(1)
您安装上传器的列应该存储上传文件的“标识符”。默认情况下,它只是文件名,但您可以将其覆盖为除记录 ID 之外的几乎任何内容(因为直到保存后您才能知道它是什么)。
要覆盖:在您的上传器类中,添加以下定义:
在本例中,我使用了模型中的created_on属性。如果您想创建自己的存储机制,那么您需要能够通过此标识符唯一地标识文件,因此请谨慎选择。
我建议重命名该列,以便它描述正在上传的文件(就像在载波示例中一样)。然后您可以随时将标识符从文件名更改为其他名称。
The column you mount the uploader on is supposed to store an "identifier" for the uploaded file. By default it's just the filename, but you can override it to be almost anything apart from the ID of the record (because you can't know what that is until after saving).
To override: in your uploader class, add this definition :
In this example I've used the created_on attribute from the model. If you want to create your own storage mechanism then you need to be able to uniquely identify files by this identifier so be careful what you choose.
I would suggest renaming the column so it describes the file that's being uploaded (like in the carrierwave example). Then you can always change the identifier from filename to something else later.