【问题标题】:How to change email address with Devise on rails3.1如何在 Rails3.1 上使用 Devise 更改电子邮件地址
【发布时间】:2011-10-03 15:09:09
【问题描述】:

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

我想要以下流程:

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

我该怎么做?

【问题讨论】:

标签: ruby-on-rails devise


【解决方案1】:

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

添加到 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.

【讨论】:

    猜你喜欢
    • 2011-05-10
    • 2011-09-09
    • 1970-01-01
    • 2020-08-16
    • 2016-10-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-15
    相关资源
    最近更新 更多