简体   繁体   中英

Heroku rake task file path error

I have data I need to seed my heroku app with for production. When I run the heroku run rake db:seed command I get an error, "rake aborted! no such file in directory"

I've tried creating a custom rake task and I get the same error.

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. 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:

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

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 . What you would probably see, provided your file is located at db/seeds.rb is: /home/username/Apps Folder/my_app/seed_file.txt .

See this previous question for more information about __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. 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

# 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.

Try CSV.foreach("./seed_file.txt") instead.

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. there should be a seeds.rb file in the db folder of your rails app where you can configure your database seeding. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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