2

When a user signs up with Devise, I want to check if their email address contains @mysite.com, and if so, create a new record in a table with the new user's ID.

I've been looking through the Devise docs for a way to take action after a user signs up (since I need their ID for associations), but all I've found is after_sign_up_path_for which is just a path.

Any ideas?

 class RegistrationsController < Devise::RegistrationsController
   def create
     super

     # I was hoping "super" creates the new user, and now I would have access to current_user.id, but that doesn't appear to be true.
   end
 end
Don P
  • 60,113
  • 114
  • 300
  • 432
  • possible duplicate of [Can I execute custom actions after successful sign in with Devise?](http://stackoverflow.com/questions/4753730/can-i-execute-custom-actions-after-successful-sign-in-with-devise) – Joe Kennedy Jun 19 '14 at 00:31
  • I don't want to define my own redirect path. If there is a way to use their solution without overriding the redirect path, then yes their solution would work. – Don P Jun 19 '14 at 00:46
  • Couldn't you just use call `super` after the end of `after_sign_up_path_for`? Or will that not work? – Joe Kennedy Jun 19 '14 at 01:12

2 Answers2

2

If you need to execute a function after the user logs in, Devise provides an after_database_authentication callback method. You would use the callback in the user model, not in the controller, with the advantage that you don't have to override the Devise Registrations controller.

See the documentation for DatabaseAuthenticatable.

I cover some similar techniques in my Rails Devise Tutorial, but in the tutorial I only mention the after_database_authentication callback method in passing to point out that it's not necessary to override the Devise Registrations controller to accommodate a post-sign-up action.

Daniel Kehoe
  • 10,952
  • 6
  • 63
  • 82
0

I copied the following structure from one of my applications:

class Users::RegistrationsController < Devise::RegistrationsController
  after_filter :do_something, only: :create

  def create
    ...

    super

    ...
  end

  def do_something
    # User succesfully created?
    return unless @user.id

    ... @user.id
  end
end
Danny
  • 5,945
  • 4
  • 32
  • 52