繁体   English   中英

Rails 3设计忘了密码与多个电子邮件

[英]Rails 3 Devise Forgot Password w/ Multiple Emails

我有我的Rails 3设置w / Devise但稍微扭曲:我将所有用户的电子邮件存储在emails表中,每个用户可以有多个电子邮件。 我遇到了忘记密码功能的问题。 我知道我必须覆盖一些Devise用于查找用户电子邮件然后发送密码重置的方法,但我不知道从哪里开始。 我非常感谢您提供给我的任何建议。

Devise从模型方法'email'获取电子邮件地址。 因此,如果您使用电子邮件模型将所有电子邮件存储在电子邮件表中,则可以在用户模型中定义“电子邮件”方法,并从电子邮件表中返回地址。

class User < ActiveRecord::Base
  devise :database_authenticatable, :recoverable, :rememberable, :authentication_keys => [ :login ], :reset_password_keys => [ :login ]
  has_many :emails
  ...
  def email
    emails.map{|record| record.email }
  end
end

见我的回答对一个类似问题。 您在Devise::Mailer创建一个覆盖headers_for的邮件程序,以便将其发送到多个电子邮件:

def headers_for(action)
  #grab the emails somehow
  @emails = resource.emails.map{|email| email.column_name}
  if action == :reset_password_instructions
    headers = {
      :subject       => translate(devise_mapping, action),
      :from          => mailer_sender(devise_mapping),
      :to            => @emails,
      :template_path => template_paths
    }
  else
    # otherwise send to the default email--or you can choose just send to all of them regardless of action.
    headers = {
      :subject       => translate(devise_mapping, action),
      :from          => mailer_sender(devise_mapping),
      :to            => resource.default_email,
      :template_path => template_paths
    }
  end

  if resource.respond_to?(:headers_for)
    headers.merge!(resource.headers_for(action))
  end

  unless headers.key?(:reply_to)
    headers[:reply_to] = headers[:from]
  end
  headers
end

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM