[英]Heroku rake task file path error
我有需要為我的heroku應用植入種子進行生產的數據。 當我運行heroku run rake db:seed命令時,出現錯誤,“ rake已中止!目錄中沒有此類文件”
我嘗試創建自定義rake任務,但遇到相同的錯誤。
我的瑞克任務的開始如下:
require 'csv'
CSV.foreach("/Users/username/Documents/Apps Folder/myapp/seed_file.txt") do |row|
...
end
不知道我的問題是我的CSV文件位於何處,還是其他問題,我已經查看了關於stackoverflow的其他相關問題,但他們無法解決我的問題。 有誰知道如何解決這個問題?
編輯:當我嘗試運行以下自定義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)
無需輸入文件的當前路徑(看起來是OSX
的文件目錄結構,請嘗試執行類似
File.expand_path("../../seed_file.txt", __FILE__)
它將為您提供相對於當前文件的路徑,並與../../seed_file.txt
路徑連接並經過處理將返回實際的absolute path
。 如果您的文件位於db/seeds.rb
,您可能會看到的是: /home/username/Apps Folder/my_app/seed_file.txt
。
有關__FILE__
更多信息,請參見前面的問題
編輯 :由於您的文件路徑中有空格,請嘗試以下操作:
require 'shellwords'
File.expand_path("../seed_file.txt", __FILE__).shellescape
編輯#2 :我認為您誤解了我的答案。 在這里,我將使用另一種方法重新解釋它。
因此,如果我的文件目錄如下所示:
├───App
| └───db
| ├───seeds.rb
| ├───seed_file.txt
然后,將其輸入到seed.rb文件中
# seeds.rb
CSV.foreach( File.join(File.dirname(__FILE__), '../seed_file.txt') ) do |row|
puts row
end
如果仔細閱讀錯誤消息,您將意識到Heroku將沒有/Users/username ...
目錄。
嘗試改用CSV.foreach("./seed_file.txt")
。
從文件路徑來看,您似乎正在嘗試從位於本地系統而非Heroku的文件中播種。 Rails應用程序的db文件夾中應該有一個seed.rb文件,您可以在其中配置數據庫種子。 嘗試將您的seed_file.txt添加到rails項目文件夾中,可能是app > assets > files > seed_file.txt
或類似名稱,然后改為引用該文件路徑。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.