简体   繁体   English

lib文件未加载(Rails 5)

[英]lib files are not loading (Rails 5)

I am having trouble loading modules from the lib folders of my application. 我无法从应用程序的lib文件夹加载模块。 Here is the the file/module that i am trying to load: 这是我要加载的文件/模块:

在此处输入图片说明

# 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 } 我试过#config.eager_load_paths << "#{Rails.root}/lib"Dir[Rails.root.join('lib/**/*.rb')].each { |f| require f } Dir[Rails.root.join('lib/**/*.rb')].each { |f| require f } inside config/application.rb , but still it is not working. 在config / application.rb中Dir[Rails.root.join('lib/**/*.rb')].each { |f| require f } ,但仍然无法正常工作。

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 . 我还尝试了config.eager_load_paths << "#{Rails.root}/lib" ,还尝试将/ lib的所有文件移动到app / lib中,如此处建议的那样: https : //github.com/rails/rails/issues / 13142 But still none of them works! 但是它们仍然没有起作用!

Btw, by not working, I mean I got this error: 顺便说一句,由于不工作,我的意思是我得到了这个错误:

undefined method is_empty' for #:0x007f923c3c4b20>` #:0x007f923c3c4b20>的undefined method is_empty'

Basically Rails cannot find the method that I have defined inside the Reusable module that I am trying to load. 基本上,Rails无法在我尝试加载的Reusable模块中找到我定义的方法。

Is there any steps that I am missing? 我缺少任何步骤吗? Or something problematic on Rails side? 还是在Rails方面有问题?

The file is loading. 文件正在载入。 If it was not, you would get an uninitialized constant Reusable error instead of an undefined method error. 如果不是这样,则会得到uninitialized constant Reusable错误,而不是undefined method错误。

If you're trying to call: 如果您要致电:

Reusable.is_empty(value)

Then is_empty needs to be a class method. 然后is_empty需要是一个类方法。 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. 根据您的喜好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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