Mic*_*ael 6 database heroku ruby-on-rails-3
我正在尝试在 Rails 应用程序中播种数据,尤其是使用载波的应用程序。我正在使用在 stackoverflow 上找到的 2 个不错的解决方案的组合!
我有以下几点:
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
Run Code Online (Sandbox Code Playgroud)
但是在heroku上,我不断收到有关路径的错误,例如
rake aborted!
No such file or directory - app/assets/images/objects/house.svg
Run Code Online (Sandbox Code Playgroud)
我已经尝试了该行的几个版本:
item.filename.store!(File.open(File.join(Rails.root, "app/assets/images/objects/" + item.name.gsub(/ /,'').downcase + ".svg")))
Run Code Online (Sandbox Code Playgroud)
我基本上改变了使用 File.join 和只是串联,并尝试使用 RAILS_ROOT 上面似乎工作正常,但我似乎总是得到这个错误。
您是否在其中创建任何文件app/assets/images/objects/或以其他方式将数据放入其中?您的 git 存储库中是否存在该目录?您可以通过heroku run ls app/assets/images/objects/从命令行运行来检查该目录是否存在于 heroku 上。
在heroku 上运行的所有应用程序都使用临时文件系统:
\n\n\n\n\n每个 dyno 都有自己的临时文件系统,以及最近部署的代码的新副本。在 dyno\xe2\x80\x99s 生命周期内,其正在运行的进程可以使用文件系统作为临时暂存器,但写入的文件对于任何其他 dyno 中的进程都是不可见的,并且在 dyno 停止或停止时,写入的任何文件都将被丢弃重新启动。
\n
这意味着,除非您在app/assets/images/objects/git 存储库内调用的目录中有文件,或者您在部署程序后以编程方式创建这些文件,否则实际上可能不存在任何此类文件或具有该名称的目录。