5

I need to add some simple methods and actions when a new user registers through Devise.

I want to apply a notify method which will send an email to me.

I want to use acts_as_network to pass a session value and connect the new register to the person who invited them.

How do I customize, I looked at the docs, but I'm not entirely clear what I need to do....thanks!

Satchel
  • 16,414
  • 23
  • 106
  • 192
  • Have you looked at this post? http://stackoverflow.com/questions/3546289/override-devise-registrations-controller – mbreining May 01 '11 at 23:39
  • @feelway, no I haven't, but it looks promising...I guess "super" means it is inheriting from the Devise controller? – Satchel May 02 '11 at 06:07
  • Yes. Super will executes the code from the inherited method. – dombesz May 02 '11 at 09:55
  • @feelnoway, this looks like it should be the right answer, thanks....can you put as answer so I can accept (and also if I have more questions for you post them?) – Satchel May 14 '11 at 18:14

1 Answers1

15

This is what I'm doing to override the Devise Registrations controller. I needed to catch an exception that can potentially be thrown when registering a new User but you can apply the same technique to customize your registration logic.

app/controllers/devise/custom/registrations_controller.rb

class Devise::Custom::RegistrationsController < Devise::RegistrationsController
  def new
    super # no customization, simply call the devise implementation
  end

  def create
    begin
      super # this calls Devise::RegistrationsController#create
    rescue MyApp::Error => e
      e.errors.each { |error| resource.errors.add :base, error }
      clean_up_passwords(resource)
      respond_with_navigational(resource) { render_with_scope :new }
    end
  end

  def update
    super # no customization, simply call the devise implementation 
  end

  protected

  def after_sign_up_path_for(resource)
    new_user_session_path
  end

  def after_inactive_sign_up_path_for(resource)
    new_user_session_path
  end
end

Note that I created a new devise/custom directory structure under app/controllers where I placed my customized version of the RegistrationsController. As a result you'll need to move your devise registrations views from app/views/devise/registrations to app/views/devise/custom/registrations.

Also note that overriding the devise Registrations controller allows you to customize a few other things such as where to redirect a user after a successful registration. This is done by overriding the after_sign_up_path_for and/or after_inactive_sign_up_path_for methods.

routes.rb

  devise_for :users,
             :controllers => { :registrations => "devise/custom/registrations" }

This post may offer additional information you might be interested in.

Community
  • 1
  • 1
mbreining
  • 7,739
  • 2
  • 33
  • 35