【问题标题】:rails, devise recoverable not sending emailsrails,设计可恢复的不发送电子邮件
【发布时间】:2014-11-28 06:36:35
【问题描述】:

我已经在我的 Rails 应用程序中进行了设计。我添加了可确认的,并发送了电子邮件以确认电子邮件帐户,并且它在开发环境中正确发送。

我也添加了可恢复选项:

  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable, :confirmable

但是当我转到“忘记密码”并输入电子邮件时,它不会发送任何内容。我真的很难弄清楚为什么可恢复的电子邮件不起作用。我很确定这不是我的电子邮件设置,因为它可以确认。有人可以提供有关调试的任何想法吗?或者从哪里开始研究为什么可恢复无法正常工作?

非常感谢。

我在忘记密码时按下提交按钮时的日志:

Started POST "/users/password" for 127.0.0.1 at 2014-11-28 01:54:07 -0500
  ActiveRecord::SchemaMigration Load (1.0ms)  SELECT "schema_migrations".* FROM "schema_migrations"
Processing by PasswordsController#create as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"bGrcwyIqwkeC51TOLyoDqX1bTj2XKNH9RHU7qJ01Zfs=", "user"=>{"email"=>""}, "commit"=>"Reset Password"}
  Rendered devise/passwords/new.html.erb within layouts/application (8.8ms)
  Rendered layouts/_header.html.erb (4.2ms)

这是我注册新用户时的日志(可确认):

 Rendered devise/mailer/confirmation_instructions.html.erb (1.4ms)

Devise::Mailer#confirmation_instructions: processed outbound mail in 33.6ms

Sent mail to test19@test.com (223.6ms)
Date: Fri, 28 Nov 2014 02:29:45 -0500
From: test
Reply-To: test
To: test19@test.com
Subject: Confirmation instructions
Mime-Version: 1.0
Content-Type: text/html;
 charset=UTF-8
Content-Transfer-Encoding: 7bit

为什么无法恢复激活 devise::mailer?

我的用户.rb:

class User < ActiveRecord::Base

  extend FriendlyId
  friendly_id :username, use: [:slugged, :finders]

  def should_generate_new_friendly_id?
    username_changed?
  end

  #validates :username, :uniqueness => {:case_sensitive => false},
  #:format => { with: /\A[a-zA-Z0-9]+\Z/ }

  validates :firstname, presence: true
  validates :lastname, presence: true
  validates :country, presence: true
  validates :birthday, presence: true

  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable, :confirmable

  attr_accessor :login

  validates_with AttachmentSizeValidator, :attributes => :avatar, :less_than => 1.megabytes

  acts_as_voter
  acts_as_messageable
  acts_as_votable
  is_impressionable

  has_attached_file :avatar, :styles => { 
      :medium => "300x300>", 
      :thumb => "100x100#", 
      :small => "50x50#",
      :tiny => "35x35>",
      :header => "30x30#" }, 
      default_url: ActionController::Base.helpers.image_path('silhouette.png')

  validates_attachment_content_type :avatar, :content_type => /\Aimage\/.*\Z/

   def self.find_first_by_auth_conditions(warden_conditions)
      conditions = warden_conditions.dup
      if login = conditions.delete(:login)
        where(conditions).where(["lower(username) = :value OR lower(email) = :value", { :value => login.downcase }]).first
      else
        where(conditions).first
      end
    end
end

【问题讨论】:

  • 您在请求发送密码重置指令时能否发送回溯/服务器日志?日志可以帮助我帮助您解决问题
  • 当然——当我点击提交按钮时,我刚刚发布了我在控制台中看到的内容。感谢您的帮助 - 让我知道您需要查看的其他任何内容。
  • 兄弟,说实话,这只是一小部分。但是,如果您稍等片刻,您将看到控制台中显示的电子邮件消息。您需要提供更多详细信息服务器日志:)
  • 嗯,谢谢 ruby​​rider。我刚刚检查了日志,我发布的内容实际上是我点击提交按钮时唯一发生的事情……它根本没有调用 devise::mailer……你会碰巧知道它在哪里被调用吗?

标签: ruby-on-rails devise passwords


【解决方案1】:

您是否在用户表中添加了可恢复模块所需的正确字段?如果没有,则运行此迁移

class AddRecoverableToUsers < ActiveRecord::Migration
  def change
    add_column :users, :reset_password_token, :string
    add_column :users, :reset_password_sent_at, :datetime
  end
end

运行后重启服务器再试一次

或者,在devise/password/new.html.erb 中的form_for 标记内添加这些行,您将在视图中看到确切的错误

<% if f.object.errors.any? %>
  <ul>
    <% f.object.errors.full_messages.each do |msg| %>
      <li><%= msg %></li>
    <% end %>
  </ul>
<% end %>

所以,由于您的 username 字段正在产生问题,您可以添加一个 before_update 回调,试试这个

before_validation :check_for_reset_token, if: Proc.new{ |user| user.reset_password_token_changed? }

def check_for_reset_token
  self.username = username
end

【讨论】:

  • 嗨 RSB - 是的,我的用户表中已经有了这些字段。我只是不确定为什么当用户点击提交时它没有调用邮件程序?
  • 你重启服务器了吗?
  • 是的,好几次。每次我点击提交时,它只会呈现我在原始问题中发布的内容。
  • 当您输入要恢复的电子邮件时显示什么内容?
  • 什么都没有——它只是刷新到同一页面。提交后URL从localhost:3000/users/password/new变为localhost:3000/users/password
【解决方案2】:

只是为了确定一下,您能否确认这些步骤:

确认你的 config/environments/developement.rb 中有这个

   config.action_mailer.default_url_options = { host: 'localhost:3000' }
   config.action_mailer.delivery_method = :smtp
   config.action_mailer.perform_deliveries = true

您能否再次检查您在运行迁移时是否取消了所有这些行的注释:

   t.string   :confirmation_token
   t.datetime :confirmed_at
   t.datetime :confirmation_sent_at
   t.string   :unconfirmed_email # Only if using reconfirmable

我也会在 config/environments/developement.rb 中推荐这个

config.action_mailer.raise_delivery_errors = true

【讨论】:

  • 你好新鲜-谢谢。确认我在 development.rb 中拥有所有前 3 行。我的问题是:recoverable,而不是:confirmable。这个问题真的很奇怪,因为我的 :confirmable 工作得很好,但 :recoverable 没有。 =/
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-07-07
  • 1970-01-01
  • 1970-01-01
  • 2013-05-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多