Expected to define. When calling class inside a module

后端 未结 6 1868
星月不相逢
星月不相逢 2020-12-15 05:12

I new to rails. I have a setup in the lib directory like so:

lib/
   blog/
     core/
        search/
            base.rb

The base.rb defin

6条回答
  •  悲哀的现实
    2020-12-15 05:22

    I had the same problem. It comes from the fact that you try to load /lib/blog/core/search/base.rb directly in application.rb with /lib/**/

    Error I had:

    Expected /[...]/myapp/lib/durative/base.rb to define Base (LoadError)
    

    Directory structure:

    lib/
     --durative/
       --base.rb
    

    base.rb:

    module Durative
      class Base
        def initialize(config)
           @config = {}
        end
        #...
      end
    end
    

    application.rb:

    config.autoload_paths += Dir["#{config.root}/lib/**/"]
    

    Here are the changes I made to make it work

    Directory structure:

    lib/
     --durative.rb **(added)**
     --durative/
       --base.rb
    

    durative.rb:

    require 'durative/base'
    

    base.rb (no change)

    application.rb (changed):

    config.autoload_paths += Dir["#{config.root}/lib/"]
    

    Tell us if it worked for you too.

提交回复
热议问题