如何在rails配置路径中访问yaml

squ*_*ter 5 ruby gem yaml ruby-on-rails

我正在写一个ruby gem,我想让用户使用你自己的yaml文件配置,但我不知道如何测试user_config.yml是否存在于rails配置路径中.
我只想在配置路径中存在此文件时才使用user_config.yml.

Jer*_*man 13

Rails.root(或RAILS_ROOT在早期版本的Rails中)为您提供应用程序根目录的路径.从那里,您可以看到您感兴趣的文件是否存在.

例如

path = File.join(Rails.root, "config", "user_config.yml")
if File.exists?(path)
  # do something
end
Run Code Online (Sandbox Code Playgroud)

  • 这些天你可能会使用`Rails.root.join('config','user_config.yml')` (2认同)