Frank Levering
Frank Levering

Reputation: 401

Uninitialized constant inside controller namespace

I'm trying to solve the following issue but I just can't get my head around the autoloading in Rails yet. I have the following controller located in app/api/v1/registration_controller.

class Api::V1::RegistrationController < ApplicationController
   include ::Foo::Bar

   def index
   end
 end

And the following module/class in app/foo/bar.rb:

module Foo
  class Bar
     def some_method
     end
  end
end

However, I'm getting the following error message:

uninitialized constant Foo

I'm trying to get my head around this and already searched Stack Overflow for solutions, but I wasn't able to find the right one. If the solution is already written down elsewhere, please close this topic.

Hopefully someone can help me. Thanks!

Upvotes: 3

Views: 608

Answers (1)

Chris Heald
Chris Heald

Reputation: 62648

The Rails autoloader searches a number of paths to try to locate constants - app/ isn't one of them. You can add it by appending it in application.rb, like:

config.autoload_paths << File.join(config.root, "app")

Or just move foo/bar.rb into lib/, which is already in the loader path.

Upvotes: 2

Related Questions