RoR - Which is preferred - Rack Middleware or Active Controller Filters?
Asked Answered
V

2

11

For the latest version of Ruby on Rails (4 at the time of asking this question), what is the preferred way to implement code that modifies the request/response such as an authentication mechanism. I see many sites and tutorials advocating Rack middleware while it seems like the same functionality can be achieved through Action Controller filter methods.

In addition to talking about the preferred methodology, can a comparison of the strengths and weaknesses of each be provided? In my initial investigation, it would seem that action controller filter methods are more tightly integrated into a RoR app such that you can bypass running certain filters on certain controller endpoints while middleware does not seem to be able to have that level of control. Details like this would be great. Thanks!

Vullo answered 10/1, 2015 at 6:38 Comment(0)
C
15

Rack Middleware and ActionController Filters are really quite different.

Rack is the standard Ruby webserver interface. It is designed to work in such a way that Rack applications or "Middlewares" can be chained together, each transforming the request / response in a particular way. If you create/use a Rack Middleware you are getting a chance to transform the request prior to it actually reaching the Rails app.

ActionController Filters are simply before/after hooks that execute before or after your immediate controller methods in Rails. These will be invoked immediately before or after your controller method, but after the entire remainder of the Rails stack.

Therefore there are significant differences in what is possible via a Rack Middleware and an ActionController filter, namely, because a Rack Middleware is executing prior to your application code it will not be executed in the same scope as your application code -- e.g. you won't be able to use your app models etc. unless you explicitly require them and perform the necessary initialization (like establishing a database connection).

If you're looking for rules of thumb, off the top of my head here's what I would tell you:

  1. If you want to do something with the request before methods only in a specific controller, use a before filter in that controller.

  2. If you want to do something with the request before all controller methods in your app, and what you want to do is very specific to your application or relies on your application code, use a filter on your ApplicationController.

  3. If you want to do something generic with the request, not tied to your application code at all, and you imagine it would be nice to be able to re-use that in another app, a Rack Middleware would be a better fit.

Hope that helps.

Caruthers answered 17/1, 2015 at 2:24 Comment(0)
C
0

As far as I understand Action Controller filters and Rack middlewares are doing pretty much the same thing, except that two things:

  • Rack middleware is called before the Rails stack, so that you can gain some performance (not sure about that though)
  • Rack middleware is self-contained and reusable between different Rack apps, so that your Rails/Engine controllers and Sinatra/Merb/Padrino apss are clean and DRY
Cotswolds answered 10/1, 2015 at 7:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.