gpt4 book ai didi

ruby-on-rails - 如何使用 Devise on rails3.1 更改电子邮件地址

转载 作者:行者123 更新时间:2023-12-03 17:34:39 25 4
gpt4 key购买 nike

我想要一个“编辑个人资料”页面,用户可以在其中更改注册时注册的电子邮件地址。

我想有以下流程:

  • 用户在更改电子邮件字段之前必须输入密码进行确认。
  • 提交该页面后,用户应该会收到一封验证邮件,就像 Devise 的默认注册邮件一样。
  • 一旦用户点击邮件上的验证 token URL,电子邮件更改就完成了。

我该怎么做?

最佳答案

我为我的网站创建了同样的流程。以下是您可以执行的操作的示例:

添加到 config/routes.rb(请注意,路由可能会更好,但我前阵子这样做了)

scope :path => '/users', :controller => 'users' do
match 'verify_email' => :verify_email, :as => 'verify_email'
match 'edit_account_email' => :edit_account_email, :as => 'edit_account_email'
match 'update_account_email' => :update_account_email, :as => 'update_account_email'
end

添加到 app/controllers/users_controller.rb

def edit_account_email
@user=current_user
end

def update_account_email

@user=current_user
@user.password_not_needed=true
@user.email=params[:address]

if @user.save
flash[:notice]="your login email has been successfully updated."
else
flash[:alert]="oops! we were unable to activate your new login email. #{@user.errors}"
end
redirect_to edit_user_path

end

def verify_email

@user=current_user
@address=params[:address]

UserMailer.confirm_account_email(@user, @address).deliver

end

app/mailers/user_mailer.rb

class UserMailer < ActionMailer::Base

def confirm_account_email(user, address)

@user = user
@address = address

mail(
:to=>"#{user.name} <#{@address}>",
:from=>"your name <'your_email@domain.com'>",
:subject=>"account email confirmation for #{user.name}"
)

end

end

app/views/user_mailer/confirm_account_email.html.erb

<p>you can confirm that you'd like to use this email address to log in to your account by clicking the link below:</p>

<p><%= link_to('update your email', update_account_email_url(@user, :address=>@address)) %></p>

<p>if you choose not to confirm the new address, your current login email will remain active.

关于ruby-on-rails - 如何使用 Devise on rails3.1 更改电子邮件地址,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7636884/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com