Rails 5: autoloading with broken custom folders

I have this class:

# app/events/new_request.rb
class Events::NewRequest
end

      

And I added this folder to startup:

# config/application.rb
config.autoload_paths += %W( events/ )

      

And at startup rails c

:

> Events::NewRequest
NameError: uninitialized constant Events

      

The thing is, if I don't use the Events namespace when defining a class, it automatically loads the class.

+3


source to share


1 answer


module Sandbox
  class Application < Rails::Application
    config.autoload_paths += [config.root.join('app')]
  end
end

      

This will allow Rails to autoload Events::NewRequest

from app/events/new_request.rb

.



irb(main):001:0> Events
=> Events
irb(main):002:0> Events::NewRequest
=> Events::NewRequest

      

+2


source







All Articles