Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where should I place my middleware file for Rails 5.1?

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?

like image 200
Siva Avatar asked Jun 21 '17 23:06

Siva


1 Answers

Create a folder app/middlewares and create your middleware file in this folder.

But unfortunately The app/middlewares 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.

like image 184
Abhi Avatar answered Nov 03 '22 01:11

Abhi