How do I add instance variables to Devise email templates?

雨燕双飞 提交于 2019-12-23 09:28:03

问题


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

Current the beginning of the file reads...

Hi, <%= @email %>,

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

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)

Thanks


回答1:


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

class ConfirmationsMailer < Devise::Mailer
  default from: '<no-reply@example.com>'

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

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!



来源:https://stackoverflow.com/questions/27698242/how-do-i-add-instance-variables-to-devise-email-templates

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!