user984621
user984621

Reputation: 48511

Rails - how to load the whole directory of files in /lib?

I have in the lib directory following structure:

/lib/dir_a/dir_b/dir_c/

In dir_c are stored images.

I am trying to load these images and display them in views. I have tried to set up the path to the images in views, but I got the 404 error.

So I did following: created a file in the initializers folder and into this file I put:

Dir[Rails.root + 'lib/dir_a'].each do |file|
  require file
end

For loading all content stored in the dir_a directory (which involves as subdirectories as files). But when I restarted server, I got this error:

...dependencies.rb:251:in `require': cannot load such file -- /Users/radek/rubydev/EDI/lib/brands (LoadError)

I also tried stuff like

Dir[Rails.root + 'lib/dir_a/'].each do |file|

or

Dir[Rails.root + 'lib/dir_a/**'].each do |file|

But none of those helped me.

Thus, is there any way to load a content from /lib directory and work with them in views?

Upvotes: 0

Views: 322

Answers (1)

Mori
Mori

Reputation: 27789

Dir[Rails.root + 'lib/**/*.rb'].each do |file|
  require file
end

Upvotes: 3

Related Questions