如何跳过使用设计确认电子邮件地址更新的需要?

AnA*_*ice 20 ruby-on-rails devise ruby-on-rails-3

我正在使用Rails 3和新的设计确认.

我想让用户通常确认他们的电子邮件地址.创建新用户时,我可以跳过确认电子邮件:

user.skip_confirmation!
Run Code Online (Sandbox Code Playgroud)

但是,有时我需要代表用户手动更改电子邮件.这在改变电子邮件时似乎不起作用.例:

@user = User.find_by_email('bob@site.com')
@user.email = 'dead@site.com'
@user.skip_confirmation!
@user.save!
Run Code Online (Sandbox Code Playgroud)

这仍然需要用户确认电子邮件.电子邮件没有更新.设计正在发送一封电子邮件.

有任何想法吗?谢谢

Rai*_*DFW 66

对于更新,你可以坚持使用skip_reconfirmation!,或只是一个普通的skip_reconfirmation!skip_reconfirmation!(注意是" 重"的确认).

@user = User.find_by_email('bob@site.com')
@user.email = 'dead@site.com'
@user.skip_reconfirmation!
@user.save!
Run Code Online (Sandbox Code Playgroud)


Rod*_*res 12

尝试设置Devise.reconfirmableUser.reconfirmable(或任何你的模型)为假.你可以config/initializers/devise.rb在这一行上设置它:

# If true, requires any email changes to be confirmed (exctly the same way as
# initial account confirmation) to be applied. Requires additional unconfirmed_email
# db field (see migrations). Until confirmed new email is stored in
# unconfirmed email column, and copied to email column on successful confirmation.
config.reconfirmable = true
Run Code Online (Sandbox Code Playgroud)

您还可以使用Active Record的update_column方法,该方法可以在不运行回调或验证的情况下保存字段.


Vla*_*mir 5

您可以在模型中使用此类代码# models/users.rb.这将禁用更新时的电子邮件重新确认:

def postpone_email_change?
  false 
end
Run Code Online (Sandbox Code Playgroud)

或在您的控制器中

def update
  user.email = params[:user][:email]
  user.skip_confirmation_notification!

  if user.save
    user.confirm
  end
end
Run Code Online (Sandbox Code Playgroud)