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)
function to read the file.
what is the relative path to the file?
To solve this, you'll need to use a built-in Rails helper called current_page? . With the current_page? helper you can pass it things like a relative or absolute path that you'd like to check or you can even pass it actions and controllers with parameters for it to check. Doing so will return either a true or false .
file = File.join(Rails.root, 'app', 'csv', 'names.csv') File.read(file)
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With