Ruby-on-rails 获取 Rails 公用文件夹内文件的绝对路径

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3323471/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 22:57:07  来源:igfitidea点击:

Getting absolute path to the file inside the public folder in Rails

ruby-on-railsfile

提问by Vincent

What is the best way to get absolute path to the file inside the public folder in controller? Is there a predefined Rails variable that holds absolute path to the public folder?

获取控制器公用文件夹内文件绝对路径的最佳方法是什么?是否有预定义的 Rails 变量保存公共文件夹的绝对路径?

Currently I'm using File.expand_path('../../../public', __FILE__), but I'm sure there's a nicer way of doing this.

目前我正在使用File.expand_path('../../../public', __FILE__),但我确信有更好的方法来做到这一点。

回答by William

Rails.public_pathshould provide you the public path

Rails.public_path应该为您提供公共路径