Ryzal Yusoff
Ryzal Yusoff

Reputation: 1047

lib files are not loading (Rails 5)

I am having trouble loading modules from the lib folders of my application. Here is the the file/module that i am trying to load:

enter image description here

# app/lib/reusable.rb    
module Reusable

  # Check if the value is completely blank & empty
  def is_empty(value)
  end

end

I have tried putting #config.eager_load_paths << "#{Rails.root}/lib" or Dir[Rails.root.join('lib/**/*.rb')].each { |f| require f } inside config/application.rb , but still it is not working.

I also have tried config.eager_load_paths << "#{Rails.root}/lib" and also tried moving all the file of /lib into app/lib like suggested here: https://github.com/rails/rails/issues/13142 . But still none of them works!

Btw, by not working, I mean I got this error:

undefined methodis_empty' for #:0x007f923c3c4b20>`

Basically Rails cannot find the method that I have defined inside the Reusable module that I am trying to load.

Is there any steps that I am missing? Or something problematic on Rails side?

Upvotes: 0

Views: 851

Answers (1)

jvillian
jvillian

Reputation: 20263

The file is loading. If it was not, you would get an uninitialized constant Reusable error instead of an undefined method error.

If you're trying to call:

Reusable.is_empty(value)

Then is_empty needs to be a class method. Something like:

# app/lib/reusable.rb    
module Reusable

  # Check if the value is completely blank & empty
  def self.is_empty(value)
  end

end

Or:

# app/lib/reusable.rb    
module Reusable

  # Check if the value is completely blank & empty
  class << self

    def is_empty(value)
    end

  end

end    

Depending on your preferences.

Upvotes: 2

Related Questions