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)
file = File.join(Rails.root, 'app', 'csv', 'names.csv')
File.read(file)
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'))
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
Thanks for above answers, It also worked this way for me:
"#{Rails.root}/public/spreadsheets/file_name.xlsx"