我试图在 Devise 邮件程序发送的确认页面中显示用户密码。确认页面为默认页面
Welcome [email protected] /cdn-cgi/l/email-protection!
You can confirm your account email through the link below:
Confirm my account
然而,我希望有
Welcome [email protected] /cdn-cgi/l/email-protection!
Your password is currently DASADSADS
You can confirm your account email through the link below:
Confirm my account
如何访问视图中的用户对象?我是否需要用自定义控制器覆盖邮件控制器?如果是这样,我如何知道当前邮件程序的方法是做什么的(尝试查看文档但找不到任何线索)?
我注意到视图中使用了@email 和@resource。我可以使用其中任何一个来访问未散列形式的当前密码吗?
请注意,我正在手动发送此电子邮件user.find(1).send_confirmation_instructions
虽然这可以做到,但我要警告非常强烈反对这样做。哈希密码是专门使用的,因此密码无法轻易重新创建。将原始密码传回给用户将导致其以纯文本形式发送回,这有点违背了整个目的。另外,用户不应该已经知道他们的密码吗(毕竟他们确实输入了两次)?!?
为此,您需要在注册中捕获原始(未散列)密码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
或者,您可以从派生一个新控制器Devise::RegistrationsController
并将此覆盖代码放在那里(推荐的方法 - 但话又说回来,这整个操作并不真正推荐)。您需要添加unhashed_password
为此工作的访问器:
class User < ActiveRecord::Base
attr_accessor :unhashed_password
end
然后您可以更新您的确认视图(位于app/views/devise/mailer/confirmation_instructions.html.erb
)来包含这个:
<p>Your password is currently <%= @resource.unhashed_password %></p>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)