如何覆盖 CarrierWave 存储图像的位置? def store_dir 似乎不起作用
我正在从 Attachment_fu 迁移到 Carrierwave,因为 Attachment_fu 在 Rails 3 中被破坏了,并且希望能够使用我已有的 Attachment_fu 图像文件,而不是 Carrierwave 制作新图像文件并将它们移动到其他地方。
我的图像按 ID 分区,因此如果用户 ID 为 61
并且他们上传名为 "foo.png"
的文件,那么他们的旧 Attachment_fu 图像将位于 “public/images/users/0000/0061/foo.png”
在我的用户模型中,我安装了 Carrierwave 图像上传器:
attr_accessible :user_avatar
mount_uploader :user_avatar, UserAvatarUploader
在我的 UserAvatarUploader
中,我有这个:
def store_dir
File.join Rails.root, "public/images/users", ("%08d" % model.id).scan(/\d{4}/).join("/")
end
所以当我设置了 :user_avatar
,我应该得到 "public/images/users/0000/0061/foo.png"
但是当我尝试设置 :user_avatar< /code> 将
User
模型中的旧图像
user.user_avatar = "#{Rails.root}/public/images/users/0000/0061/foo.png"
返回为类似的内容"/uploads/tmp/20110916-1244-15398-7724/foo.png"
似乎 store_dir
没有被正确覆盖。我需要做什么才能使这项工作正常进行?
I'm moving from attachment_fu to carrierwave, since attachment_fu is broken with Rails 3, and would like to be able to use the attachment_fu image files that I already have, instead of carrierwave making new ones and moving them somewhere else.
My images are partitioned by id, so if the user id is 61
and they upload a file called "foo.png"
, then their old attachment_fu image would be at "public/images/users/0000/0061/foo.png"
In my users model, I mount the carrierwave image uploader with:
attr_accessible :user_avatar
mount_uploader :user_avatar, UserAvatarUploader
In my UserAvatarUploader
, I have this:
def store_dir
File.join Rails.root, "public/images/users", ("%08d" % model.id).scan(/\d{4}/).join("/")
end
so when I set :user_avatar
, I should get "public/images/users/0000/0061/foo.png"
but when I try to set the :user_avatar
in the User
model to the old image
user.user_avatar = "#{Rails.root}/public/images/users/0000/0061/foo.png"
it comes back as something like "/uploads/tmp/20110916-1244-15398-7724/foo.png"
It seems that store_dir
is not being overwritten properly. What do I need to do to make this work right?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Carrierwave 默认基本位置是公共文件夹,
尝试使用以下命令更改它
Carrierwave default base location is the public folder
try to change it by using