Rails: path of file

前端 未结 4 791
庸人自扰
庸人自扰 2021-02-03 19:20

I have inside app a directory called csv and inside this dir I have a file called names.csv I want to use File.read(path:string)

相关标签:
4条回答
  • 2021-02-03 19:38
    file = File.join(Rails.root, 'app', 'csv', 'names.csv')
    File.read(file)
    
    0 讨论(0)
  • 2021-02-03 19:42

    Rails.root points to the top folder of your rails project, so the path would be:

    File.read(File.join(Rails.root, 'app','csv','names.csv'))
    
    0 讨论(0)
  • 2021-02-03 19:45

    You should do: Rails.root.join "app", "csv", "names.csv"

    Rails.root returns a PathName object. PathName has a join method which takes any number of arguments and appends it to the pathname to create the new path.

    Read on PathName#join here:

    http://www.ruby-doc.org/stdlib-1.9.3/libdoc/pathname/rdoc/Pathname.html#method-i-join

    0 讨论(0)
  • 2021-02-03 19:49

    Thanks for above answers, It also worked this way for me:

    "#{Rails.root}/public/spreadsheets/file_name.xlsx"
    
    0 讨论(0)
提交回复
热议问题