DEV Community

Prathamesh Sonpatki
Prathamesh Sonpatki

Posted on • Originally published at prathamesh.tech on

5 1

prepend_before_action in Rails

Rails encourages usage of callbacks in controllers to execute common pieces of code before or after an action. A very simple example of this is calling the authenticate_user! method from Devise before every action to make sure that user is authenticated.

class ApplicationController < ActionController::Base
  before_action :authenticate_user!
end
Enter fullscreen mode Exit fullscreen mode

But what if you want to execute a different piece of code before calling authenticate_user!? I wanted to set a cookie based on whether the request was coming from mobile app or from web before calling authenticate_user!. The most important thing was I wanted to set the cookie only for a particular controller action.

class MagicAuthController < ApplicationController
  # Set cookie before authenticate_user! before auth

  def auth
  end
end
Enter fullscreen mode Exit fullscreen mode

This can be solved by multiple approaches. We can create a separate base controller for MagicAuthController and make sure the cookie is set before calling authenticate_user!

class MagicBaseController < ActionController::Base
  before_action :set_cookie, only: [:auth]
  before_action :autheticate_user!
end
Enter fullscreen mode Exit fullscreen mode

Though Rails also provides a nifty callback called prepend_before_action. It inserts the callback that is passed to it to the start of the callback chain.

class MagicAuthController < ApplicationController
  def auth
  end
end

Enter fullscreen mode Exit fullscreen mode

As the MagicAuthController inherits from the ApplicationController the callback chain consists of authenticate_user! as of now.

class MagicAuthController < ApplicationController
  prepend_before_action :set_cookie, only: [:auth]

  def auth
  end
end
Enter fullscreen mode Exit fullscreen mode

This changes the callback chain and now set_cookie is the first callback in the callback chain for the auth action.

Rails also provides other friends of prepend_before_action such as prepend_after_action and prepend_around_action.

It also accepts a block argument.

prepend_before_action only: [:auth] { cookies[:clid] = params[:clid] }
Enter fullscreen mode Exit fullscreen mode

<!--kg-card-end: code--><!--kg-card-begin: hr-->


Want to know more about such tricks and tips from Rails? Subscribe to my newsletter and keep up with Ruby and Rails.

Hostinger image

Get n8n VPS hosting 3x cheaper than a cloud solution

Get fast, easy, secure n8n VPS hosting from $4.99/mo at Hostinger. Automate any workflow using a pre-installed n8n application and no-code customization.

Start now

Top comments (0)

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay