简体   繁体   中英

require bindata in ruby on rails application

I have a set of files that are in the lib directory of a Ruby on Rails application.

I have a model that needs to use these files. In my model I have the following:

require_relative '../../some_path_to_file_without_extention'

(Side note; I would love to know a way to require all the files, instead of require_relative for each file).

The file that I require_relative has the following require in it.

require "bindata"

When I try to access functions from the require_relative file I get the following error:

LoadError: cannot load such file -- bindata

This is happening for other gems that are being required in the set of files as well. I just chose bindata as an example.

I have bindata in my Gemfile. When I run bundle show bindata it shows me the path to bindata .

I even put require 'bindata' in my model, but it gave me the same load error.

How do I stop the LoadError ?

Any help would greatly be appreciated.

Update 1

When I run bundle show . I get the following:

Gems included by the bundle:
  ...
  * bcrypt (3.1.11)
  * bindata (2.3.4)
  ...

Then in the console, requiring bcrypt works but bindata does not.

irb(main):002:0> require 'bcrypt'
=> true

But bindata does not.

irb(main):003:0> require 'bindata'
LoadError: cannot load such file -- bindata

Update 2

Ok so I know is has to be something with how I am loading my rails environment.

bundle exec irb
irb(main):001:0> require 'bindata'
=> true

Update 3

So I went back a few git commits and keep trying to add the gems and see if they would load in my rails console. I went back far enough were it did. Did not know what was different. However, I also noticed when my spring server was restarted then my gems would load in my rails console.

To require all the files in the lib folder, add config.autoload_paths << Rails.root.join('lib') to your application.rb , this answer can help you: https://stackoverflow.com/a/19650564/3739191

Now,

require 'bindata'

should work :)

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