Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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!

like image 508
PressingOnAlways Avatar asked Jan 10 '15 06:01

PressingOnAlways


People also ask

What method is called in rack which is also called in all Rack middleware?

Rack automatically calls the call method on the middleware and expects back a [status, headers, body] array, just like our JSONServer returns. So in this middleware, the start point is taken, then the actual call to the JSONServer is made with @app.

What is rack in ROR?

Rack provides a minimal, modular, and adaptable interface for developing web applications in Ruby. By wrapping HTTP requests and responses in the simplest way possible, it unifies and distills the API for web servers, web frameworks, and software in between (the so-called middleware) into a single method call.

What is Before_filter in Ruby on Rails?

Before Filters ADVERTISEMENT. ADVERTISEMENT. Rails before filters are executed before the code in action controller is executed. The before filters are defined at the top of a controller class that calls them. To set it up, you need to call before_filter method.

What is the role of rails controller?

The Rails controller is the logical center of your application. It coordinates the interaction between the user, the views, and the model. The controller is also a home to a number of important ancillary services. It is responsible for routing external requests to internal actions.


1 Answers

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.

like image 152
Andrew Avatar answered Sep 22 '22 20:09

Andrew