Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tips on using with Devise #41

Open
andrewhavens opened this issue Aug 30, 2019 · 4 comments
Open

Tips on using with Devise #41

andrewhavens opened this issue Aug 30, 2019 · 4 comments
Assignees

Comments

@andrewhavens
Copy link

I would like to style my Devise email templates using the same layout that is used by the other emails in my application, but in order to do this, I will need to call the make_bootstrap_mail method from within the Devise::Mailer class somehow. I think there are a few different ways to achieve this, but I'm curious if anyone else has dealt with this before and how did you do it?

@swrobel
Copy link
Contributor

swrobel commented Sep 5, 2019

If you come up with a clever solution, I'd love to hear it!

@andrewhavens
Copy link
Author

andrewhavens commented Sep 5, 2019

This is the solution I came up with (updated to use part of @aaronchi's code below):

  1. Generate a new mailer class: bin/rails g mailer UserMailer

  2. Update your new mailer class with this:

    class UserMailer < Devise::Mailer
      layout 'bootstrap-mailer'
      default template_path: 'devise/mailer'
    
      def devise_mail(record, action, opts = {}, &block)
        initialize_from_record(record)
        make_bootstrap_mail headers_for(action, opts), &block
      end
    end
  3. Update the mailer config setting in config/initializers/devise.rb:

    config.mailer = 'UserMailer'
  4. Assuming you already generated the devise views (using rails g devise:views), the email templates live in app/views/devise/mailer

@stuyam stuyam self-assigned this Nov 11, 2019
@aaronchi
Copy link

aaronchi commented Dec 13, 2019

Monkey Patch:

module Devise
  module Mailers
    module Helpers
      # Bootstrap Email Override
      def devise_mail(record, action, opts = {}, &block)
        initialize_from_record(record)
        make_bootstrap_mail headers_for(action, opts), &block
      end
    end
  end
end

@WoodyDark
Copy link

I'd like to bring the discussion back to life in regards to using Devise with Bootstrap Email in Rails 6. I have a pretty similar setup as everyone here and followed @aaronchi's monkey patch. The one possible difference I made is that I added config.parent_mailer = 'ApplicationMailer' in devise.rb and now when I start my rails server, I get a deprecation warning. Does anyone know what this really means and how would I fix it? (or how did you guys fix it?)

DEPRECATION WARNING: Initialization autoloaded the constants Devise::Mailer, ApplicationHelper, and UserMailer.

Being able to do this is deprecated. Autoloading during initialization is going
to be an error condition in future versions of Rails.

Reloading does not reboot the application, and therefore code executed during
initialization does not run again. So, if you reload Devise::Mailer, for example,
the expected changes won't be reflected in that stale Class object.

These autoloaded constants have been unloaded.
# devise.rb

...
config.mailer = UserMailer
config.parent_mailer = 'ApplicationMailer'
...
# user_mailer.rb

class UserMailer < Devise::Mailer
  helper :application
  include Devise::Controllers::UrlHelpers
  ...
end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants