28

I'm using Devise to allow user signup as-well-as using my own user admin to create users manually. When I create a user in the admin, Devise sends a confirmation immediately to the new user. I believe this is due to the fact that both devise and my admin use the same model. How do I delay this email until the administrator is ready to send it?

Additionally, Devise's validation is requiring the admin set a password for the new user. I would much prefer the manually created users specify their own password when they respond the confirmation. Right now manually created users will not know their password unless I send it too them in a supplemental email.

Arslan Ali
  • 17,418
  • 8
  • 58
  • 76
Tim Santeford
  • 27,385
  • 16
  • 74
  • 101

7 Answers7

55

@user.skip_confirmation! confirms the user, so the user can log in without using the confirmation.

This works for me in devise 3.5

Stop devise to send confirmation email while creating user.

@user.skip_confirmation_notification! 

Send confirmation instructions later

@user.send_confirmation_instructions
Jashwant
  • 28,410
  • 16
  • 70
  • 105
  • 3
    This deserves to be the top answer. `skip_confirmation!` confirms the user automatically so its pointless for the OP's use case. – nahtnam Aug 11 '15 at 03:40
40

We do this in one of our apps. You can tell Devise NOT to automatically deliver the confirmation like this:

@user.skip_confirmation!

And then later, you can do

Devise::Mailer.confirmation_instructions(@user).deliver

For Rails 2.x you'd do something like:

DeviseMailer.deliver_confirmation_instructions(@user)
Ryenski
  • 9,582
  • 3
  • 43
  • 47
  • 4
    I should also mention that you have to set @user.skip_confirmation! _before_ you save the record. – Ryenski Jan 06 '11 at 16:41
  • 1
    as dombesz mentioned in his answer - @user.skip_confirmation!, does this : self.confirmed_at = Time.now.utc, so later you have: user.active? == true... user can do everythig as if he had confirmed his email... – santuxus Jan 12 '12 at 16:44
  • 5
    I'm using user.send_confirmation_instructions, as that will generate a confirmation token if none exists – Peter Ehrlich Jun 14 '12 at 21:55
  • 1
    Hasn't the signature of this method changed in the latest devise? it's now DeviseMailer.deliver_confirmation_instructions(@user, token) http://rubydoc.info/github/plataformatec/devise/master/Devise/Mailer:confirmation_instructions – Ryan Romanchuk Apr 03 '14 at 23:26
  • 1
    yes the signatures have changed in later versions. see http://stackoverflow.com/a/16548371/899160 for a working solution. – amlutz160 Nov 15 '14 at 17:53
  • 1
    At this point, Jashwant's answer is better for a number of reasons (calls method with more stable signature, generates confirmation token, does not confirm user who has not clicked confirm link), so what might have been the best answer nearly four years ago is definitely not the best answer now. – LisaD Dec 08 '14 at 18:19
  • When I used "Devise::Mailer.confirmation_instructions(@user).deliver" I got wrong number of arguments (1 for 2..3). I used @user.send_confirmation_instructions and it worked. Thanks a lot.. – amm Aug 09 '15 at 13:56
  • Probably better to use `@user.send_confirmation_instructions` as in @Jashwant's answer – Ryenski Aug 10 '15 at 18:08
12

There's now an easier way (was added back in v2.2.4)

Devise's confirmable module now includes the perfect skip_confirmation_notification! method which allows for a cleaner solution:

@user = User.new params[:user]
@user.skip_confirmation_notification! 
@user.save

# ... do stuff, then later...

Devise::Mailer.confirmation_instructions(@user).deliver 
amlutz160
  • 448
  • 5
  • 18
12

The solution is not so simple as @Ryan Heneise's answer. If you do @user.skip_confirmation! it confirms the user, so the user can log in without using the confirmation, so the confirmation letter in this case is useless. This solution does not allows the user to log in without the confirmation: Rails 3 with Devise for Authentication - How do I manually create a user?

Community
  • 1
  • 1
dombesz
  • 7,890
  • 5
  • 38
  • 47
1

I just spent some time looking into this, basically you just need to add this and setup delayed_job.

def send_confirmation_instructions
  generate_confirmation_token! if self.confirmation_token.nil?
  ::Devise.mailer.delay.confirmation_instructions(self)
end

Add it in your user model as protected method to override the devise one in confirmable. Also, note the gem was just updated to add a new method to be overridden on create that sends the confirmation instructions.

Matt Smith
  • 3,479
  • 2
  • 28
  • 29
-2

Do you use delayed job ? It allows you to define single methods for delayed run.

mpapis
  • 52,729
  • 14
  • 121
  • 158
  • Maybe delay was a bad word choice. I want to stop Devise from sending the email but have an action that sends that confirmation email when the admin is finished configuring the new user record. – Tim Santeford Dec 03 '10 at 08:09