0

I just upgraded to Rails 5.2 and have run into the following issues in development mode, while using a class JsonWebToken from my app/lib folder in my ApplicationController.

NameError - uninitialized constant ApplicationController::JsonWebToken:

Following the directions listed here, I have the following notable things:

  1. I'm using bootsnap that comes by default with Rails 5.2
  2. I no longer have a require at the top of ApplicationController since files within the app folder seem to be auto-required.
  3. In my development.rb, I have config.eager_load = false.
  4. In my application.rb, I have the following eager and auto loading code:

    autoloads lib & policy folder during production
    config.eager_load_paths << Rails.root.join('lib')
    config.eager_load_paths << Rails.root.join('policies')
    
    #autoloads lib & policy folder during development
    config.autoload_paths << Rails.root.join('lib')
    config.autoload_paths << Rails.root.join('policies')
    

If I remove bootsnap, then I have to add require 'JsonWebToken' at the top of ApplicationController, and then everything works. Bootsnap didn't like that require statement at the top.

I am not sure what is the right way to be creating or using your own classes in Rails 5.2, and to set up so that they are loaded properly in both development and production. I have gone through the Rails docs, but am extremely unclear.

halfer
  • 19,824
  • 17
  • 99
  • 186
geoboy
  • 1,172
  • 1
  • 11
  • 25
  • Did you run `$ spring stop`...I ran into a similar issue and this fixed it. Also, the "rails way" is to use application.rb as you have done, instead of using require statements. – Mark Merritt Jul 26 '18 at 02:35
  • Yes, spring was already stopped. In theory, does the above look good? – geoboy Jul 26 '18 at 03:47
  • Do you have an `include JsonWebToken` in your `ApplicationController`? Try to change it to `include ::JsonWebToken`. – Denny Mueller Aug 07 '18 at 06:29

0 Answers0