Frontend Developers: I'm redesigning DevTools. Want early access?

Run code during the Devise 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 this approach, skim my other post first. Let’s begin:

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.

2. 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.

Frontend Developers: I'm redesigning DevTools. Want early access?