Rails 4.1 Mailer Previews and Devise Custom Emails

Rails 4.1 Mailer Previews and Devise custom emails

I found this question because I was trying to figure out how to preview Devise emails myself. I copied your code almost exactly and it works fine for me.

The only thing I did differently was to remove the line layout "notifications_mailer" from UserMailer - when I included it I got an error message Missing template layouts/notifications_mailer. What happens if you remove it?

The line Devise::Controllers::UrlHelpers definitely includes the method confirmation_url (you can see this for yourself by opening up the Rails console and running include Devise::Controllers::UrlHelpers then confirmation_url, so I suspect the problem is that your previews aren't being rendered by UserMailer at all. I'm not sure why, but the line layout "notifications_mailer" might be the culprit.

Do you have a class called NotificationsMailer? Does including Devise::Controllers::UrlHelpers in there solve your problem?

Preview devise mailer not found

Ok I found out why.

Because I'm using the gem rspec-rails the repository test is replaced tp spec
I just needed to move my test/mailers/previews/devise/mailer_preview.rbto spec/mailers/previews/devise/mailer_preview.rb and still not need to change the configuration of config/application.rb

Hope it will help some people

also note that in rails 6 your devise mailer preview should be wrote with a module

module Devise
class MailerPreview< ActionMailer::Preview

def confirmation_instructions
Devise::Mailer.confirmation_instructions(User.first, "faketoken")
end

def reset_password_instructions
Devise::Mailer.reset_password_instructions(User.first, "faketoken")
end

end
end

Rails 4.1: access current_user in ActionMailer::Preview

What would happen if you move your mailer job to the background? How would you get the current user then?

The mailer and its preview should not know about the current_user. The mailer's job is to send the mail to a user it receives. The preview is there to visually demonstrate its behaviour.

Create a new user in your mailer preview, and pass it to the mailer.

  def new
user = User.create! # etc...
SubscriptionsMailer.new(user)
end

It doesn't matter who the user is. It matters that it's a user object.

If you want to test that the application will send a mail to the current_user, write a functional test for that.

How do I preview emails in Rails?

Action Mailer now has a built in way of previewing emails in Rails 4.1. For example, check this out:

# located in test/mailers/previews/notifier_mailer_preview.rb

class NotifierPreview < ActionMailer::Preview
# Accessible from http://localhost:3000/rails/mailers/notifier/welcome
def welcome
Notifier.welcome(User.first)
end
end

Rails: I have devise confirmable installed but I want a seperate mailer for contact me and other features

Yes, you can have separate mailer, and it is better to have separate mailer instead of making devise handle all mail.

ActionMailer basics: http://guides.rubyonrails.org/action_mailer_basics.html



Related Topics



Leave a reply



Submit