如何将实例变量添加到Devise电子邮件模板中

如何将实例变量添加到Devise电子邮件模板中

本文介绍了如何将实例变量添加到Devise电子邮件模板中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编辑confirmation_instructions.html.erb文件,以便通过名字而不是电子邮件来解决新用户的问题.

I am trying to edit the confirmation_instructions.html.erb file to address the new user by first name instead of email.

当前文件的开头读取...

<%= @email%>,

Current the beginning of the file reads...

Hi, <%= @email %>,

如何将实例变量@first_name添加到控制器/邮件程序?

How do I add an instance variable @first_name to the controller/mailer?

我运行rails生成devise:controllers用户,但是我看不到任何.rb文件可以在其中将实例变量添加到确认邮件中(如果有的话)

I ran rails generate devise:controllers users, but I just don't see any .rb files where I could add instance variables to confirmation mailer (if there is one)

谢谢

推荐答案

这很简单,您可以为此创建一个Mailer:

This is easy, you can just create a Mailer for this:

class ConfirmationsMailer < Devise::Mailer
  default from: '<[email protected]>'

  def confirmation_instructions(record, token, opts={})
   @token = token
   #you can add your instance variables here
   devise_mail(record, :confirmation_instructions, opts)
  end
end

然后告诉Devise使用此类:

And then just tell Devise to use this class:

config/initializers/devise.rb

Devise.setup do |config|
...
config.mailer = 'ConfirmationsMailer'
...
end

重新启动服务器,您应该一切顺利!

Restart your server, and you should be good to go!

这篇关于如何将实例变量添加到Devise电子邮件模板中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 03:48