Run code during the Devise registration process

Mark Thomas Miller /

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.