How to run code during Devise's registration process

This post will show you how to run code when creating a user in the Devise registration process. In my opinion, this should be a last resort — if you’re considering editing Devise’s defaults, you might want to look at my other post, which provides an easier alternative:

Don’t touch Devise’s defaults

If you’d like to continue, this is how to run code during the Devise registration process:

  1. Add a new controller at app/controllers/registrations_controller.rb:
class RegistrationsController < Devise::RegistrationsController

private

def sign_up(resource_name, resource)
puts "This user's email is #{resource.email}."
sign_in(resource_name, resource)
end
end

Devise automatically runs sign_up when the user submits the registration form. You can access the current user as resource, and we need to call sign_in per the original RegistrationsController.

  1. Tell Devise to use this controller inside config/routes.rb.
devise_for :users, :controllers => {
registrations: 'registrations'
}

And there you go! That’s all you need to do to run code during registration. Again, you might want to skim my other post before committing to a custom registration process because it can open up a can of worms.

About the author

I'm Mark Thomas Miller, a full stack engineer and designer currently working at ConvertKit. (We're hiring!) People like Arnold Schwarzenegger, Lindsey Stirling, and Tim Ferriss use features I've built to connect with their fans. I'm currently geeking out about Svelte, mechanical keyboards, and minimalist UI design, and replaying Ocarina of Time.