Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails: path of file

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?

like image 469
socksocket Avatar asked Oct 30 '12 20:10

socksocket


People also ask

How do I find the path in rails?

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 .


2 Answers

file = File.join(Rails.root, 'app', 'csv', 'names.csv') File.read(file) 
like image 188
tamersalama Avatar answered Sep 22 '22 18:09

tamersalama


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

like image 33
user566245 Avatar answered Sep 23 '22 18:09

user566245