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

爱⌒轻易说出口 提交于 2020-01-12 14:24:50

问题


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


回答1:


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).




回答2:


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')


来源:https://stackoverflow.com/questions/8714645/how-to-specify-the-path-of-the-public-directory-in-a-ruby-on-rails-app

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!