An alternative to Rails before_action

Read the most recent version of this article in my personal blog.

Rails before_action is a good tool but often used incorrectly. In this article, I will show what is wrong and my remedy for before_action abuse.

To illustrate this I invented one task.

There is a Ruby on Rails project. The task is to determine whether a page visitor has access to a controller method before the method execution. The ideal task for a controller filter, now called before_action in Rails. But I need the data from the filter in the controller method itself. I do not want to make a second request for the same data again. I do not want to clutter up the session and do not want to assign an instance variable in the controller filter.

With the constraint above I produced the solution below:

The #customer_special_offer_code block acts as a filter and at the same time as a source of the required data offer_code.

I did a check, acquire the data, set no instance variables in the controller scope and have no extra database queries. Job done!

Very often the before_action method is used so that it only harms the code. For example, this “Best practice” is wrong in my opinion in two reasons:

  • controller inheritance is no longer an option, together with inherited methods, you will get all filters; although I do not recommend to consider inheritance with any resource controllers, ApplicationController, AdminController, and similar controllers are the exception.

Following the statements above, I do not recommend to do this:

As an alternative, I suggest using a block with an explicit returned value. Here is what I mean:

In my opinion, the block method is more clean and simple solution for the data instantiation problem rather than the before_action approach.

If you have opinion, suggestions, feedback on this topic please drop a comment. I will be happy to have a talk about it.

If you liked what you read please clap or share the article.

Say Hi to me on twitter 👋

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store