1

Every time a controller or model tries to access a class on the /lib folder it says:

NameError (uninitialized constant 'current_controller':'class_name' did you mean 'something_else')

YES, I know the rails naming conventions and I am using it correctly. I have the code running in several other servers (Ubuntu & CentOS 6). It errors only on these 2 RedHat7.2 servers we have - same exact ruby/rails/gems on all servers. The error occurs with any library file I try to use. SELinux is disabled.

Ruby version 2.3.3; Rails version 5.1.0 (same on all servers)

Anyone have any ideas? Rails is suppose to automatically load those class files.

pirhac
  • 887
  • 2
  • 8
  • 16

1 Answers1

3

On rails < 5:

config/application.rb

module your_app
  class Application < Rails::Application
    # Settings in config/environments/* take precedence over those specified here.
    # Application configuration should go into files in config/initializers
    # -- all .rb files in that directory are automatically loaded.
    config.autoload_paths += %W(#{config.root}/lib/path)
  end
end

on rails >= 5

module your_app
  class Application < Rails::Application
    config.eager_load_paths << "#{Rails.root}/lib/path"
  end
end

If you want rails to auto load your lib you have to place it under the app folder.

Community
  • 1
  • 1
m3characters
  • 2,240
  • 2
  • 14
  • 18
  • Won't you still need to add `lib` to the `autoload_paths` in rails 5? I believe `eager_load_paths` will only take effect when `eager_load` is set to true, which won't be true in development. In rails 5, rails will not fallback to autoloading in production if `eager_load` is true, so you do need to add the directory to eager_load_paths as well. – Puhlze Jul 17 '17 at 22:24
  • @Puhlze at least with 5.1.2 I only have it with `eager_load_paths`, double checked `config.eager_load` and is set to false on `development.rb`. The classes won't be eager_loaded, but the paths available?? Not sure what black magic is at play there and won't look at it right now :p – m3characters Jul 17 '17 at 22:34
  • My application.rb had to be updated to the rails 5 standard. I don't understand why in SOME servers it still worked with the rails 4 standard. Now everyone has the rails 5 standard and everyone is happy :) – pirhac Jul 18 '17 at 12:59