Previously I had my middleware under lib/middleware/my_middle_ware.rb
However when doing this,
config.middleware.use MyMiddleWare
I receive a
NameError: uninitialized constant
Where is rails looking for the middleware?
Previously I had my middleware under lib/middleware/my_middle_ware.rb
However when doing this,
config.middleware.use MyMiddleWare
I receive a
NameError: uninitialized constant
Where is rails looking for the middleware?
Look like rails wasn't looking for it.
I had to do the following for it to work.
Dir["./lib/middleware/*.rb"].each do |file|
require file
end
middleware
, like there is for models
, controllers
, views
, mailers
, etc. –
Micromho Create a folder app/middlewares
and create your middleware file in this folder.
But unfortunately The app/middleware
s folder is not loading even if I added to the load paths in Rails v5.2.2
config.autoload_paths << "#{Rails.root}/app/middlewares"
config.eager_load_paths << "#{Rails.root}/app/middlewares"
So you can use require explicitly as follows, add this line in application.rb
require_relative '../app/middlewares/my_middleware'
and load middleware:
config.middleware.use MyMiddleware
and call rake middleware
to see the middleware stack.
I believe you want to add your middleware to either your config/application.rb
or your config/environments
file.
config.middleware.use MyMiddleWare
This should work and append MyMiddleWare
to the bottom of the middleware stack.
Even before app/middleware contents are loaded if 'config.middleware.use' is called, I think you get the 'uninitialized Constant error'. The below should fix
config.middleware.use "MyMiddleWare"
If the above doesn't work, one of the below might be a no.
Is MyMiddleWare in app/middleware/my_middle_ware.rb ?
Is MyMiddleWare in lib/my_middle_ware.rb ?
replacing middleware as a string in config/application.rb
to config/environment/{environment}
as a constant fixed the issue for me
© 2022 - 2024 — McMap. All rights reserved.