Rails: uninitialized constant just happen on production server
Asked Answered
S

1

7

I have a class that I put inside lib/network:

module NetworkApi
  class NetworkProxy
  end
end

Then in another class, I referenced this class:

  network_proxy = ::NetworkApi::NetworkProxy.new(params)

Everything runs normally on my development environment, but when I deploy to the server, I get an error at the above line with the message:

NameError: uninitialized constant NetworkApi::NetworkProxy

I don't know why this strange error happens. Please tell me why.

Stegman answered 10/1, 2017 at 2:25 Comment(1)
guides.rubyonrails.org/…Degrade
Z
14

Note that Rails 5 disables autoloading after booting the app in production.

From the linked blog post:

In the rare situation where our application still needs autoloading in the production environment, we can enable it by setting up enable_dependency_loading to true as follows:

# config/application.rb 

config.enable_dependency_loading = true
config.autoload_paths << Rails.root.join('lib')`
Zooplankton answered 10/1, 2017 at 2:30 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.