Expected to define. When calling class inside a module

后端 未结 6 1861
星月不相逢
星月不相逢 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:23

    I had the same issue. The problem was because I was including subdirs without including their parent lib dir:

    # did not work
    config.autoload_paths += %W(#{config.root}/lib/foo)
    

    and

    # in lib/foo/my_class.rb
    module Foo
      class MyClass
      end
    end
    

    Foo::MyClass would return Expected to define MyClass

    adding the lib dir to config.autoload_paths fixes the problem

    # worked
    config.autoload_paths += %W(#{config.root}/lib #{config.root}/lib/foo)
    

提交回复
热议问题