简体   繁体   中英

Rails: how to load local file into the model?

I have a file located in /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. How do I load this file?

I found out what the issue was.

RAILS_ROOT is depreciated. You're supposed to use Rails.root instead.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM