简体   繁体   English

Heroku如何使用file.open和rake填充图像载波

[英]Heroku how to populate images carrierwave using file.open and rake

  rake db:populate (works)
  heroku run rake db:populate (works to some extent)

I ran into a problem when I tried to populate the database on heroku. 当我尝试在heroku上填充数据库时遇到了一个问题。 Locally the following works perfectly and my website is populated with images. 在本地,以下各项均能正常工作,并且我的网站中装有图像。

  namespace :db do
     desc "Fill database with sample data"
     task populate: :environment do
     .
     .
     @uploader = ImageUploader.new(@movie_project, :image)
     @uploader.store!(File.open("app/assets/images/photos/picture.jpg"))
     @movie_project.image = @uploader

All MovieProjects are successfully populated with data(title, genre etc). 所有MovieProjects均已成功填充数据(标题,流派等)。 Its only the image that doesn't get created on heroku. 它只是在heroku上未创建的图像。 I suspect that this have something to do with the 我怀疑这与

  File.open

Tried things like changing directory but without success, maybe I just use the wrong directory 尝试过诸如更改目录之类的事情,但没有成功,也许我只是使用了错误的目录

  app/assets/images => public/assets/ ?

I use cedar stack, rails 3.2, Carrierwave and the assets are precompiled before uploaded. 我使用雪松栈,rails 3.2,Carrierwave,并且资产在上载之前进行了预编译。 This is just for testing purpose to test with different layouts. 这仅用于测试目的,以测试不同的布局。 Creating a MovieProject and assign a picture works on Heroku, but using the above methods doesn't seems to. 创建一个MovieProject并分配图片可在Heroku上使用,但似乎无法使用上述方法。

// //

Thanks Jon 谢谢乔恩

Ran into the same problems tonight, and fixed it the following way: 今晚遇到同样的问题,并通过以下方式解决了该问题:

Firstly, make sure you have something like this in your uploader or initializer: 首先,请确保您的上传器或初始化程序中包含以下内容:

config.cache_dir = "#{Rails.root}/tmp/uploads"

Now, use carrierwave's cache! 现在,使用carrierwave的cache! method, instead of store! 方法,而不是store! . I also call save! 我也叫save! afterwards 然后

 @uploader.cache!(File.open("app/assets/images/photos/picture.jpg"))
 @movie_project.image=@uploader
 @movie_project.save!

Hope this helps, let me know if it doesn't 希望这会有所帮助,让我知道是否可以

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM