[英]Heroku rake task file path error
I have data I need to seed my heroku app with for production. 我有需要为我的heroku应用植入种子进行生产的数据。 When I run the heroku run rake db:seed command I get an error, "rake aborted! no such file in directory"
当我运行heroku run rake db:seed命令时,出现错误,“ rake已中止!目录中没有此类文件”
I've tried creating a custom rake task and I get the same error. 我尝试创建自定义rake任务,但遇到相同的错误。
The beginning of my rake task is as follows: 我的瑞克任务的开始如下:
require 'csv'
CSV.foreach("/Users/username/Documents/Apps Folder/myapp/seed_file.txt") do |row|
...
end
Not sure if my issue is where I have my CSV file located, or if it's something else, I've looked at other related questions on stackoverflow and they haven't been able to fix my problem. 不知道我的问题是我的CSV文件位于何处,还是其他问题,我已经查看了关于stackoverflow的其他相关问题,但他们无法解决我的问题。 Does anyone know how to solve this issue?
有谁知道如何解决这个问题?
EDIT: I've added the error message I receive in terminal when I try to run my custom rake task below: 编辑:当我尝试运行以下自定义rake任务时,我添加了在终端中收到的错误消息:
Running `rake customraketask` attached to terminal... up, run.8243
rake aborted!
No such file or directory -/Users/username/Documents/Apps Folder/myapp/app/assets/files/seed_file.txt
/app/lib/tasks/customraketask.rake:13:in `block in <top (required)>'
Tasks: TOP => customraketask
(See full trace by running task with --trace)
Instead of typing in your current path to the file (which looks to be the file directory structure of OSX
, try doing something like 无需输入文件的当前路径(看起来是
OSX
的文件目录结构,请尝试执行类似
File.expand_path("../../seed_file.txt", __FILE__)
Which would give you a path relative to the current file, concatenated with the ../../seed_file.txt
path and processed will return the actual absolute path
. 它将为您提供相对于当前文件的路径,并与
../../seed_file.txt
路径连接并经过处理将返回实际的absolute path
。 What you would probably see, provided your file is located at db/seeds.rb
is: /home/username/Apps Folder/my_app/seed_file.txt
. 如果您的文件位于
db/seeds.rb
,您可能会看到的是: /home/username/Apps Folder/my_app/seed_file.txt
。
See this previous question for more information about __FILE__
有关
__FILE__
更多信息,请参见前面的问题
EDIT : Since you have spaces in your filepath, try the following: 编辑 :由于您的文件路径中有空格,请尝试以下操作:
require 'shellwords'
File.expand_path("../seed_file.txt", __FILE__).shellescape
EDIT #2 : I think you've misunderstood my answer. 编辑#2 :我认为您误解了我的答案。 I'll re-explain it here using a different method.
在这里,我将使用另一种方法重新解释它。
So, if my file directory looks like this: 因此,如果我的文件目录如下所示:
├───App
| └───db
| ├───seeds.rb
| ├───seed_file.txt
Then I would type this into my seeds.rb file 然后,将其输入到seed.rb文件中
# seeds.rb
CSV.foreach( File.join(File.dirname(__FILE__), '../seed_file.txt') ) do |row|
puts row
end
If you read the error message carefully, you would realize the Heroku would not have your /Users/username ...
directory. 如果仔细阅读错误消息,您将意识到Heroku将没有
/Users/username ...
目录。
Try CSV.foreach("./seed_file.txt")
instead. 尝试改用
CSV.foreach("./seed_file.txt")
。
judging from your file path, it looks like you are trying to seed from a file that is located on your local system, not at Heroku. 从文件路径来看,您似乎正在尝试从位于本地系统而非Heroku的文件中播种。 there should be a seeds.rb file in the db folder of your rails app where you can configure your database seeding.
Rails应用程序的db文件夹中应该有一个seed.rb文件,您可以在其中配置数据库种子。 try adding your seed_file.txt to the rails project folder maybe
app > assets > files > seed_file.txt
or something similar and reference that file path instead. 尝试将您的seed_file.txt添加到rails项目文件夹中,可能是
app > assets > files > seed_file.txt
或类似名称,然后改为引用该文件路径。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.