Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails: uninitialized constant just happen on production server

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.

like image 427
Trần Kim Dự Avatar asked Jan 10 '17 02:01

Trần Kim Dự


1 Answers

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')`
like image 142
wjordan Avatar answered Nov 03 '22 05:11

wjordan