如何从heroku上的rake任务访问文件?

时间:2022-08-03 02:32:33

I am trying to seed data in a Rails app, particularly one using carrierwave. I am using a combination of 2 nice solutions found here on *!

我试图在Rails应用程序中播种数据,特别是使用carrierwave的应用程序。我正在使用*上的2个很好的解决方案的组合!

rake task + yml files for seeding

用于播种的rake task + yml文件

seeding carrierwave

I have the following:

我有以下内容:

namespace :db do
  desc "This loads the development data."
  task :seed => :environment do
    require 'active_record/fixtures'
    Dir.glob(RAILS_ROOT + '/db/fixtures/*.yml').each do |file|
      base_name = File.basename(file, '.*')
      puts "Loading #{base_name}..."
      ActiveRecord::Fixtures.create_fixtures('db/fixtures', base_name)
    end
    #add in the image file for the default data
    for item in Item.find(:all)
      item.filename.store!(File.open(File.join(Rails.root, "app/assets/images/objects/" + item.name.gsub(/ /,'').downcase + ".svg")))
      item.save!
    end
  end

  desc "This drops the db, builds the db, and seeds the data."
  task :reseed => [:environment, 'db:reset', 'db:seed']
end

However on heroku, I keep getting errors about the path such as

但是在heroku上,我不断收到有关路径的错误,例如

rake aborted!
No such file or directory - app/assets/images/objects/house.svg

I have tried several versions of the line:

我尝试了几个版本的产品线:

item.filename.store!(File.open(File.join(Rails.root, "app/assets/images/objects/" + item.name.gsub(/ /,'').downcase + ".svg")))

where I basically changed out using File.join and just concatenation and also trying to use the RAILS_ROOT which seemed to work ok above, but I always seem to get this error.

我基本上改变了使用File.join和连接,并尝试使用RAILS_ROOT似乎上面工作正常,但我似乎总是得到这个错误。

1 个解决方案

#1


3  

Are you creating any files in app/assets/images/objects/ or otherwise putting data there? Does that directory exist in your git repo? You can check if the directory exists on heroku by running heroku run ls app/assets/images/objects/ from the command line.

您是在app / assets / images / objects /中创建任何文件还是以其他方式放置数据?该目录是否存在于您的git仓库中?您可以通过从命令行运行heroku run ls app / assets / images / objects /来检查heroku上是否存在该目录。

All apps running on heroku use an ephemeral filesystem:

在heroku上运行的所有应用程序都使用短暂的文件系统:

Each dyno gets its own ephemeral filesystem, with a fresh copy of the most recently deployed code. During the dyno’s lifetime its running processes can use the filesystem as a temporary scratchpad, but no files that are written are visible to processes in any other dyno and any files written will be discarded the moment the dyno is stopped or restarted.

每个dyno都有自己的短暂文件系统,并带有最近部署的代码的新副本。在dyno的生命周期中,其运行进程可以将文件系统用作临时暂存器,但是任何其他dyno中的进程都不会看到所写的文件,并且在dyno停止或重新启动时,所写的任何文件都将被丢弃。

What this means is that unless you have files in a directory called app/assets/images/objects/ inside your git repository or you're creating those files programatically after your program is deployed, there may not actually be any such files or a directory by that name.

这意味着除非您在git存储库中的app / assets / images / objects /目录中有文件,或者在部署程序后以编程方式创建这些文件,否则实际上可能没有任何此类文件或目录用这个名字。

#1


3  

Are you creating any files in app/assets/images/objects/ or otherwise putting data there? Does that directory exist in your git repo? You can check if the directory exists on heroku by running heroku run ls app/assets/images/objects/ from the command line.

您是在app / assets / images / objects /中创建任何文件还是以其他方式放置数据?该目录是否存在于您的git仓库中?您可以通过从命令行运行heroku run ls app / assets / images / objects /来检查heroku上是否存在该目录。

All apps running on heroku use an ephemeral filesystem:

在heroku上运行的所有应用程序都使用短暂的文件系统:

Each dyno gets its own ephemeral filesystem, with a fresh copy of the most recently deployed code. During the dyno’s lifetime its running processes can use the filesystem as a temporary scratchpad, but no files that are written are visible to processes in any other dyno and any files written will be discarded the moment the dyno is stopped or restarted.

每个dyno都有自己的短暂文件系统,并带有最近部署的代码的新副本。在dyno的生命周期中,其运行进程可以将文件系统用作临时暂存器,但是任何其他dyno中的进程都不会看到所写的文件,并且在dyno停止或重新启动时,所写的任何文件都将被丢弃。

What this means is that unless you have files in a directory called app/assets/images/objects/ inside your git repository or you're creating those files programatically after your program is deployed, there may not actually be any such files or a directory by that name.

这意味着除非您在git存储库中的app / assets / images / objects /目录中有文件,或者在部署程序后以编程方式创建这些文件,否则实际上可能没有任何此类文件或目录用这个名字。