[英]Rails: how to load local file into the model?
I have a file located in /lib/dir/file.xml
我在
/lib/dir/file.xml
有一个文件
I'm trying to call it by: 我正在通过以下方式称呼它:
file = Nokogiri::XML(File.open('#{RAILS_ROOT}/lib/dir/file.xml'))
But I get the error, I'm not sure what I'm doing wrong. 但是我得到了错误,我不确定自己在做什么错。 The file definitely exists.
该文件肯定存在。
Errno::ENOENT: No such file or directory - #{RAILS_ROOT}/lib/dir/file.xml
I'm on rails 4 and Ruby 2 if it helps. 如果有帮助的话,我会选择4和Ruby 2。 How do I load this file?
如何加载此文件?
I found out what the issue was. 我发现了问题所在。
RAILS_ROOT
is depreciated. RAILS_ROOT
已贬值。 You're supposed to use Rails.root
instead. 您应该改用
Rails.root
。
So here's the solution for people who see this in the future: 因此,这是为将来看到这种情况的人提供的解决方案:
root = Rails.root.to_s #make sure string
file = Nokogiri::XML(File.open("#{root}/lib/dir/file.xml"))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.