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

时间:2011-06-23 04:20:05

标签: ruby-on-rails ruby-on-rails-3 devise

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

2 个答案:

答案 0 :(得分:2)

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

答案 1 :(得分:0)

查看我的answer类似问题。您在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