Devise: Unable to provide different Devise::Mailer layouts based upon resource

Created on 20 Feb 2012  路  7Comments  路  Source: heartcombo/devise

I have two different types of resources, and the layout for each email should different based upon the resource type. I've come across some code that suggests two approaches for specifying the mailer's layout.

Method 1: set the layout on the class via the application.rb file.

config.to_prepare do
  Devise::Mailer.layout 'email'
end

Method 2: create a custom mailer class that inherits from Devise::Mailer, and use that.

class DeviseMailer < Devise::Mailer
  layout 'email'
end

config.mailer = "DeviseMailer"

What I'd really like is a solution similar to the following, though I don't know that it would be possible.

config.to_prepare do
  Devise::Mailer.layout proc { |resource| resource == :admin ? 'admin_email' : 'user_email' }
end

Most helpful comment

Same process. You can alternatively configure it using config.to_prepare { Devise::Mailer.layout "your layout" } in your config/application.rb

All 7 comments

You can configure the devise_mailer per resource by overriding this: https://github.com/plataformatec/devise/blob/master/lib/devise/models/authenticatable.rb#L88

Do I just override that in my model? e.g.:

class User < ActiveRecord::Base
  def devise_mailer
    UserMailer
  end
end

def Admin < ActiveRecord::Base
  def devise_mailer
    AdminMailer
  end
end

Confirm.

Great, thanks so much! Sorry to open an issue for a non-issue. :)

Hi, what if I just want to use a layout? and not to override any methods?
thanks/Obrigado

Same process. You can alternatively configure it using config.to_prepare { Devise::Mailer.layout "your layout" } in your config/application.rb

Obrigado @josevalim, valeu!
It worked well. tx for your quick response.

Was this page helpful?
0 / 5 - 0 ratings