Ruby on rails 使用设备确认页面显示用户密码

Ruby on rails 使用设备确认页面显示用户密码,ruby-on-rails,ruby-on-rails-3,devise,Ruby On Rails,Ruby On Rails 3,Devise,我试图在Desive邮件发送的确认页面中显示用户密码。确认页面是默认页面 Welcome test0@test.com! You can confirm your account email through the link below: Confirm my account 然而,我希望有 Welcome test0@test.com! Your password is currently DASADSADS You can confirm your account email th

我试图在Desive邮件发送的确认页面中显示用户密码。确认页面是默认页面

Welcome test0@test.com!

You can confirm your account email through the link below:

Confirm my account
然而,我希望有

Welcome test0@test.com!

Your password is currently DASADSADS

You can confirm your account email through the link below:

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

我注意到视图中使用了@email和@resource。我是否可以使用其中任何一个来访问未加密的当前密码


请注意,我正在使用
用户手动发送此电子邮件。查找(1)。发送确认说明

以加密形式设计保存密码:您可以使用

生成新迁移:

$ rails g migration AddLegacyPasswordToUser legacy_password:boolean
      invoke  active_record
      create    db/migrate/20120508083355_add_legacy_password_to_users.rb
$ rake db:migrate
在以下代码中使用传统密码方法,您可以解密密码:

class User < ActiveRecord::Base

...

  def valid_password?(password)
    if self.legacy_password?
      # Use Devise's secure_compare to avoid timing attacks
      if Devise.secure_compare(self.encrypted_password, User.legacy_password(password))

        self.password = password
        self.password_confirmation = password
        self.legacy_password = false
        self.save!

      else
        return false
      end
    end

    super(password)
  end

  # Put your legacy password hashing method here
  def self.legacy_password(password)
    return Digest::MD5.hexdigest("#{password}-salty-herring");
  end
end
class用户
虽然这是可以做到的,但我强烈警告不要这样做。专门使用哈希密码,因此无法轻松重新创建密码。将原始密码传递回用户将导致它以纯文本形式发送回,这有点违背了整个目的。另外,用户是否应该已经知道他们的密码(他们毕竟输入了两次)

为此,您需要在注册
create
操作中捕获原始(未加密)密码,并在该点发送电子邮件(传递密码)。您可以通过覆盖
注册
方法来完成此操作-您可以在初始值设定项中完成此操作:

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
然后,您可以更新确认视图(位于
app/views/designe/mailer/confirmation\u instructions.html.erb
)以包含以下内容:

<p>Your password is currently <%= @resource.unhashed_password %></p>
您的密码当前为


您可以使用request.request_参数[:user][:password]在创建或更新操作中获取纯文本密码。

谢谢您我正在寻找此响应。然而,我的情况略有不同(我应该详细说明)。已在调用user.find(1).发送确认指示的同一控制器中使用user.new(…)创建了该用户。因此,我在该视图中有未更改的密码。我是否可以从自定义控制器将其传递到资源对象?@user1431282:在离开
创建
操作后,浏览器将被发送重定向到另一个路径。一旦发生这种情况,就不可能重新获取未加密的密码,因为它不会存储在任何地方,也无法进行反向工程。因此,在工作流中,唯一可以执行此操作的地方是在实际设置密码(并且可以访问未更改的版本)的同一操作中,该操作作为
create
操作的一部分发生。这是一条注释,而不是答案
<p>Your password is currently <%= @resource.unhashed_password %></p>