用设计确认页面显示用户密码

use*_*282 4 ruby-on-rails devise ruby-on-rails-3

我正在尝试在Devise邮件程序发送的确认页面中显示用户密码.确认页面是默认页面

Welcome test0@test.com!

You can confirm your account email through the link below:

Confirm my account
Run Code Online (Sandbox Code Playgroud)

但是,我希望有

Welcome test0@test.com!

Your password is currently DASADSADS

You can confirm your account email through the link below:

Confirm my account
Run Code Online (Sandbox Code Playgroud)

如何在视图中访问用户对象?我是否需要使用自定义控制器覆盖邮件控制器?如果是这样,我如何判断当前邮件的方法是什么(尝试查看文档但找不到任何线索)?

我注意到在视图中使用了@email和@resource.我可以使用其中任何一个以未散列的形式访问当前密码吗?

请注意,我手动发送此电子邮件 user.find(1).send_confirmation_instructions

Pin*_*nyM 6

虽然可以做到这一点,但我会强烈反对这样做.专门使用散列密码,因此无法轻松重新创建密码.将原始密码传回给用户将导致它以纯文本形式发回,这有点违背了整个目的.此外,用户不应该已经知道他们的密码(毕竟他们输入了两次)?!?

为此,您需要在注册create操作中捕获原始(未散列)密码,然后在该点发送电子邮件(传递密码).您可以通过覆盖sign_up方法来执行此操作 - 您可以在初始化程序中执行此操作:

class Devise::RegistrationsController < DeviseController
  def sign_up(resource_name, resource)
    sign_in(resource_name, resource)
    resource.unhashed_password = resource_params[:password]
    resource.send_confirmation_instructions
  end
end
Run Code Online (Sandbox Code Playgroud)

或者,你可以从中派生一个新的控制器Devise::RegistrationsController并将覆盖代码放在那里(推荐的方法 - 但是再次,并不是真的推荐这个整个操作).您需要添加unhashed_password访问器才能工作:

class User < ActiveRecord::Base
  attr_accessor :unhashed_password
end
Run Code Online (Sandbox Code Playgroud)

然后您可以更新您的确认视图(at app/views/devise/mailer/confirmation_instructions.html.erb)以包含此内容:

<p>Your password is currently <%= @resource.unhashed_password %></p>
Run Code Online (Sandbox Code Playgroud)