Rails]对heroku进行新的提交后,图像被删除 [英] Rails] Images erased after a new commit on heroku

查看:97
本文介绍了Rails]对heroku进行新的提交后,图像被删除的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Carrierwave上传图片,现在我的应用程序在Heroku上。
除非我推送一个新的提交给heroku,否则图像会成功上传。在推送之前上传的图像似乎在新提交进行时被删除。有人知道背后的原因以及如何解决此问题吗?

更新:
问题在于,如果没有像Amazon EC3这样的存储服务器,Heroku上使用carrierwave 。 Heroku不会将文件保存在公共文件夹中,默认情况下,carrierwave会上传。



应用/上传/ image_uploader.rb:

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

您需要将其更改为tmp文件夹中的某处以便在每次提交后保留您的图像文件。我试图将它改为

 #{Rails.root} / tmp / uploads /#{model.class.to_s。下划线} /#{mounted_as} /#{model.id}

但它不起作用。我检查了我的本地环境,并且图像成功存储在正确的位置。所以我猜如果我只是将路线设置到正确的位置,这可能会奏效。有人可以帮我解决这个问题吗?

应使用 fog Carrierwave 上传到远程存储库(例如Amazon S3)。每次用户上传文件时,文件都会自动上传到您的S3存储中,而不是Heroku。


I'm using Carrierwave to upload images, and my app is on Heroku right now. Images are uploaded successfully unless I push a new commit to heroku. Images that I uploaded before a push seem to be erased when a new commit comes in. Does anyone know the reason behind and how to fix this issue?

Update: The problem becomes, using carrierwave on Heroku without a storage server like Amazon EC3. Heroku does not save files in public folder, where carrierwave uploads by default.

app/uploaders/image_uploader.rb:

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

And you need to change this to somewhere in the tmp folder in order to preserve your image files after each commit. I tried to change it to

"#{Rails.root}/tmp/uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"

But it does not work. I checked on my local environment, and the image is stored in the right location successfully. So I guess if I just set the routes to the right location, this could work. Could someone help me with this?

解决方案

Since Heroku does not allow storing static files (unless it's associated the application itself), you should make Carrierwave upload to a remote repository (e.g. Amazon S3) using fog. Everytime a user uploads a file, the file will be automatically uploaded to your S3 storage instead of Heroku.

这篇关于Rails]对heroku进行新的提交后,图像被删除的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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