Rails Devise: after_confirmation

后端 未结 8 1556
被撕碎了的回忆
被撕碎了的回忆 2020-12-05 13:31

Is there a way to create a after_confirmation :do_something ?

The goal is to send an e-mail after the user confirms using Devise :confirmable

相关标签:
8条回答
  • 2020-12-05 13:32

    You can override the confirm! method:

    def confirm!
      super
      do_something
    end
    

    Discussion about the topic is at https://github.com/plataformatec/devise/issues/812. They say that there are no callbacks like after_confirmation :do_something because that approach would require a lot of different callbacks.

    0 讨论(0)
  • 2020-12-05 13:35

    You can override the confirm! method on your model

    class User < ActiveRecord::Base
      devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable, :confirmable
    
      def confirm!
        super
        do_something
      end
    end
    

    There is a discussion about the topic is https://github.com/plataformatec/devise/issues/812. I tried this way, and it worked great.

    0 讨论(0)
  • 2020-12-05 13:46

    I don't see that callback too, maybe you can try to override the confirmation method and call your callback there.

    def send_confirmation_instructions(attributes={})
      super(attributes)
      your_method_here
    end
    
    0 讨论(0)
  • 2020-12-05 13:47

    We're combining answers from @Bernát and @RyanJM:

    def confirm!
      super
      if confirmed_at_changed? and confirmed_at_was.nil?
        do_stuff
      end
    end
    

    This seems a bit more performance aware and safe than the two answers separately.

    0 讨论(0)
  • 2020-12-05 13:50

    according to the Devise 3.5.9 source code, you can simply define a method on the Devise Resource model, e.g.:

      class User < ActiveRecord::Base
      ...
        def after_confirmation
           do_something
        end
      end
    

    See: Devise 3.5.9 Source Code: https://github.com/plataformatec/devise/blob/d293e00ef5f431129108c1cbebe942b32e6ba616/lib/devise/models/confirmable.rb

    0 讨论(0)
  • 2020-12-05 13:51

    Rails 4:

    combining multiple answers above

      def first_confirmation?
        previous_changes[:confirmed_at] && previous_changes[:confirmed_at].first.nil?
      end
    
      def confirm!
        super
        if first_confirmation?
          # do first confirmation stuff
        end
      end
    
    0 讨论(0)
提交回复
热议问题