LoginSignup
1
1

More than 5 years have passed since last update.

[Rails 5 + device 4.2] ユーザー登録時に、確認メールが送信されない

Last updated at Posted at 2017-01-20

環境

問題

  • Railsとdeviseのバージョンを上げてテストしたところ、ユーザ登録時に確認メールが送信されなくなってしまった。

原因

Userモデルに、after_createを実装していたのが原因だった。

# models/user.rb:

class User < ActiveRecord::Base
  after_create { |record|
    hoge
  }
end

深く調べていないが、DeviseのConfirmableモジュールのafter_commitで定義した内容が、after_createによって上書きされたのかなと思っている。

# devise/lib/devise/models/confirmable.rb:

    module Confirmable
      extend ActiveSupport::Concern

      included do
        before_create :generate_confirmation_token, if: :confirmation_required?
        after_create :skip_reconfirmation_in_callback!, if: :send_confirmation_notification?
        if respond_to?(:after_commit) # ActiveRecord
          after_commit :send_on_create_confirmation_instructions, on: :create, if: :send_confirmation_notification?

対応方法

Userモデル内でafter_createの代わりにafter_create_commitを使うようにしたら、確認メールが送信されるようになった。

# models/user.rb:

class User < ActiveRecord::Base
  after_create_commit :hoge
end
1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1