【发布时间】:2020-08-01 02:15:51
【问题描述】:
当用户更改其电子邮件地址时,不会确认新电子邮件。但是设计不会更新/重置 confirm_at 字段。
我试过了:
before_update :updateEmailConfirmed, if: :email_changed?
def updateEmailConfirmed
# check if there is an unconfirmed_email
user = User.find(id)
if !user.unconfirmed_email.nil?
# set confirmed_at to nil
self.update!(confirmed_at:nil)
end
end
我了解:confirmed_at 字段用于任何确认,因此它按预期工作。但是,我使用此字段来跟踪以查看电子邮件是否已通过验证。
目前,我在我的用户模型中添加了一个名为 :email_confirmed 的额外字段,类型为 bool,我将其设置为 true/false,具体取决于当前的 :email 字段是否已经过验证。
我的问题是,Devise 模块中是否有任何内置功能可以让我在不向我的用户表中引入任何新列并修改我的用户类的情况下执行此操作。
更新1。)
以下是为我的用户模型设置的标签:
class User < ApplicationRecord
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable,
:confirmable
protected
def confirmation_required?
false
end
end
这是我的用户表的样子:
create_table "users", force: :cascade do |t|
t.string "email", default: "", null: false
t.string "encrypted_password", default: "", null: false
t.string "reset_password_token"
t.datetime "reset_password_sent_at"
t.datetime "remember_created_at"
t.integer "sign_in_count", default: 0, null: false
t.datetime "current_sign_in_at"
t.datetime "last_sign_in_at"
t.string "current_sign_in_ip"
t.string "last_sign_in_ip"
t.string "confirmation_token"
t.datetime "confirmed_at"
t.datetime "confirmation_sent_at"
t.string "unconfirmed_email"
t.boolean "verified", default: false
t.index ["confirmation_token"], name: "index_users_on_confirmation_token",
unique: true
t.index ["email"], name: "index_users_on_email", unique: true
t.index ["reset_password_token"], name:
"index_users_on_reset_password_token", unique: true
end
【问题讨论】:
-
你使用
:confirmable? -
是的,我将更新问题以显示我的用户表和用户模型。
标签: ruby-on-rails ruby devise