【问题标题】:Devise authentication - no confirmation after password recovery设计身份验证 - 密码恢复后没有确认
【发布时间】:2012-05-26 08:17:08
【问题描述】:

我在 Rails 中使用 Devise 身份验证 gem。

如何显示来自 devise.en.yml 的消息:

send_instructions: 'You will receive an email with instructions about how to reset your password in a few minutes'

密码恢复电子邮件发送后,而不是被重定向到站点的根目录?

更新:

我在 devise_controller.rb 中发现了一段有趣的代码:

def successfully_sent?(resource)
  notice = if Devise.paranoid
    resource.errors.clear
    :send_paranoid_instructions
  elsif resource.errors.empty?
    :send_instructions
  end

  if notice
    set_flash_message :notice, notice if is_navigational_format?
    true
  end
end

设置断点显示正在调用正确的行,将 :send_instructions 分配给 notice ,调用了 set_flash_message,但是我看不到这一切的结果,因为我我立即重定向到根路径。

【问题讨论】:

  • 检查 flash[:notice] 是否被分配 + 你是否有块来呈现 flash 消息 + 查看日志可能你有双重重定向
  • 我有 flash[:notice] 块,但不知道额外的重定向可能隐藏在哪里。
  • 添加你是否检查过 flash[:notice] 已分配?
  • 是的,我有条件。顺便说一句:它如何防止重定向?

标签: ruby-on-rails devise confirmation password-recovery


【解决方案1】:

查看设计的 PasswordsController 的源代码:https://github.com/plataformatec/devise/blob/master/app/controllers/devise/passwords_controller.rb#L42

您必须在您的应用中创建一个从 Devise::PasswordsController 继承的 PasswordsController,仅实现 after_sending_reset_password_instructions_path_for(resource_name) 方法,并在设置路由时告诉 devise 使用您的控制器

class PasswordsController < Devise::PasswordsController
  protected
  def after_sending_reset_password_instructions_path_for(resource_name)
    #return your path
  end
end

在路线中

devise_for :users, :controllers => { :passwords => "passwords" }

【讨论】:

  • 我已经查看了资源 - 有“send_instructions”字符串,它从未显示,但如果它在那里,应该有一个显示它的功能。今天在试验控制器时,无意中得到了这个消息,但不知道如何。
  • 可能这是另一个要问的问题。很可能当您重定向到 after_sending_reset_password_instructions_path_for 中的路径时,该路径会重定向到另一个页面并且闪存消息丢失。查看日志/development.log
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-10-30
  • 2013-06-17
  • 2011-09-27
  • 2020-08-22
  • 1970-01-01
  • 2019-06-18
  • 2011-07-04
相关资源
最近更新 更多