Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to specify the path of the public directory in a ruby on rails app?

I want to parse .csv file which is in public folder, I've tried /../'s, #{RAILS_ROOT}/public but with no success (No such file or directory error). I dunno exactly how to use Rails.public_path (Rails.public_path/filename.csv doesn't work) please help

like image 802
lukaszkups Avatar asked Jan 03 '12 15:01

lukaszkups


People also ask

How do I put the file path in Ruby?

Ruby has a method for this case. It is File::expand_path . Converts a pathname to an absolute pathname. Relative paths are referenced from the current working directory of the process unless dir_string is given, in which case it will be used as the starting point.


2 Answers

You have access to the Rails.root path, use it to get a path

Rails.root.join("public", "filename.csv")

You'll possibly have to call to_s on it depending on how you want to use the result (as a Path object or as a string).

like image 156
Femaref Avatar answered Oct 29 '22 06:10

Femaref


In Rails 4, Rails.public_path, like Rails.root, returns a stdlib Pathname object, so you can also use join with it:

 Rails.public_path.join('filename.csv')