Here is my situation, I use devise to allow users to create account on my site and manage their authentication. During the registration process I allow customers to change some options, leading to an actually different account being created but still based on the same core user resource. I would like to choose not to send a confirmation email for some of those account types. I don't care if the account do not get confirmed and user cannot log in, that's ok, no pb with that. How would I go about doing that ? Thanks, Alex
Actually it's quite easy once I dig a little deeper. Just override one method in your User model (or whatever you are using):
# Callback to overwrite if confirmation is required or not.
def confirmation_required?
!confirmed?
end
Put your conditions and job's done !
Alex
-
The mail won't be sent in an resend_confirmation or a send_reconfirmation, which are usefull ... – Augustin Riedinger Sep 13 '13 at 12:45
If you just want to skip sending the email but not doing confirmation, use:
# Skips sending the confirmation/reconfirmation notification email after_create/after_update. Unlike
# #skip_confirmation!, record still requires confirmation.
@user.skip_confirmation_notification!
If you don't want to call this in your model with a callback overwrite this method:
def send_confirmation_notification?
false
end
You can also simply add the following line of code in your controller before creating the new user:
@user.skip_confirmation!
-
1If you're overloading
create
action and usingsuper do |user|
for customization, it's too late to skip, devise already sent the email. This should go inbuild_resource
, for instance. – Mauricio Pasquier Juan Jun 4 '15 at 20:54
I don't know if Devise added this after the other answers were submitted, but the code for this is right there in confirmable.rb
:
# If you don't want confirmation to be sent on create, neither a code
# to be generated, call skip_confirmation!
def skip_confirmation!
self.confirmed_at = Time.now
end
-
2Note that you should call this before saving the user, otherwise the
Confirmation instructions
email will be sent. – davetapley Apr 25 '12 at 19:49 -
2
I was able to do something similar with the functions:
registrations_controller.rb
def build_resource(*args) super if session[:omniauth] # TODO -- what about the case where they have a session, but are not logged in? @user.apply_omniauth(session[:omniauth]) @user.mark_as_confirmed # we don't need to confirm the account if they are using external authentication # @user.valid? end end
And then in my user model:
user.rb
def mark_as_confirmed self.confirmation_token = nil self.confirmed_at = Time.now end