【问题标题】:Rails 3 Devise Forgot Password w/ Multiple EmailsRails 3 设计忘记密码,带多个电子邮件
【发布时间】:2011-09-20 21:56:27
【问题描述】:

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

【问题讨论】:

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


    【解决方案1】:

    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
    

    【讨论】:

      【解决方案2】:

      请参阅我的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
      

      【讨论】:

        猜你喜欢
        • 2013-06-23
        • 1970-01-01
        • 2016-08-31
        • 2013-10-01
        • 1970-01-01
        • 2018-08-17
        • 2011-02-19
        • 1970-01-01
        • 2015-06-22
        相关资源
        最近更新 更多