6

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

vanilla_skies
  • 415
  • 5
  • 15

1 Answers1

6

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!

kurenn
  • 1,035
  • 9
  • 11
  • Can you give me an example of how to get an attribute from the user model? For instance the following isnt working @first_name = current_user.first_name – vanilla_skies Dec 31 '14 at 06:46
  • 1
    Instead of using `current_user`, you can just use the `record` argument from the method, so in your case @first_name = record.first_name – kurenn Dec 31 '14 at 13:08
  • i came here to say that I figured it out with @first_name = record.first_name and then saw your post. thanks for your help – vanilla_skies Dec 31 '14 at 20:59
  • where would I find what is available in the opts={} – vanilla_skies Dec 31 '14 at 21:00
  • You can find it in here https://github.com/plataformatec/devise/blob/66db52ce31b5d8629f5813a1d7f03a8bc17e5d52/lib/devise/mailers/helpers.rb but is basically the headers for the email, for example subject, from, to, etc – kurenn Dec 31 '14 at 21:01
  • thanks. I also noticed you can change the subject in the devise lcoal file – vanilla_skies Dec 31 '14 at 22:09
  • You are welcome! If you found the answer useful show me some love by accept it :) thanks – kurenn Dec 31 '14 at 22:10
  • I would but I do not have enough reputation points. can you upvote the question? – vanilla_skies Jan 01 '15 at 22:33