devise icon indicating copy to clipboard operation
devise copied to clipboard

Unable to use custom views for emails

Open benbonnet opened this issue 7 years ago • 15 comments

Environment

  • Ruby 2.5
  • Rails 5.2
  • Devise 4.4.3

Current behavior

For some reason, we're having a complete custom path for views

We have to define a custom path for the mailers views. The mailer custom path is being ignored with the following, seem that template_path has no effects :

class DeviseMailer < Devise::Mailer
  prepend_view_path Rails.root.join('app', 'ui')
  include Devise::Controllers::UrlHelpers
  layout 'devise_mailer' # is in app/ui/layouts/devise_mailer.html.haml
  default template_path: 'devise_mailers' # is in app/ui/devise_mailers/...
end

The above is using the expected layout, but for the views, nothing to do, it picks the default devise views (fallbacking to the ones stored in the gem - if i'm not wrong)

The custom mailer is correctly defined in devise.rb

Expected behavior

Being able to use custom mailer views based on the tutorial in the wiki

current solution

Ended up with the following workaround :

class DeviseMailer < Devise::Mailer
  prepend_view_path Rails.root.join('app', 'ui')
  include Devise::Controllers::UrlHelpers
  layout 'devise_mailer' # is in app/ui/layouts/devise_mailer.html.haml
  
  # this below has no effect
  default template_path: 'devise_mailers' # is in app/ui/devise_mailers/...
  
  # this below enables effectively the custom views path
  def headers_for(action, opts)
      super.merge!(template_path: 'devise_mailers')
  end
end

(from here)

Is the above code okay/acceptable ? Did I miss something from the doc ?

benbonnet avatar Apr 15 '18 19:04 benbonnet

Hello @benbonnet, thanks for your report. Can you provide us a sample application that reproduces the issue in isolation? You can also use our bug report template. That would help us find the issue.

Thank you!

tegon avatar May 28 '18 21:05 tegon

@tegon hello, been able to extract the minimal working setup that exposes what i tried to describe; here : https://github.com/benbonnet/devise.custom.layout

all in all it kinda work, but feels like there might be a way to do it cleanly

thx alot for your insight

benbonnet avatar May 28 '18 22:05 benbonnet

Same here (rails 4.2), thank you for your headers_for-workaround @benbonnet !

themilkman avatar May 29 '18 12:05 themilkman

@tegon is that an acceptable setup ?

benbonnet avatar Jun 01 '18 16:06 benbonnet

@benbonnet I haven't got the change to look at it yet, sorry. I'll do it when I have some time.

tegon avatar Jun 13 '18 19:06 tegon

I experienced the same problem. By debbugging I discovered that internally there is a method that apply default values, including the default path for views, even if something different is specified! Hope that helps! Please solve...

msanfilippof avatar Jun 23 '18 21:06 msanfilippof

@benbonnet I've made some tests with your app and discovered that default has no effect here because we're explicit sending this value inside headers_for, so I think your approach is ok 👍

tegon avatar Aug 01 '18 21:08 tegon

Why did you close this issue? Someone just found a workaround but the DOC is still out of date and wrong. Even though, I don't think the workaround is the correct/acceptable approach.

msanfilippof avatar Sep 29 '18 19:09 msanfilippof

default has no effect is a strange thing, which means custom_path is a useless setting. Should this be considered a bug?

alanyeh20001 avatar Oct 08 '18 08:10 alanyeh20001

It is a bug, but seems like that unless the bug affects many people, no one cares.

msanfilippof avatar Oct 08 '18 18:10 msanfilippof

@marcosanfilippo When that happens, please open another issue. When you comment on closed issues is very hard for us to keep track of it. It's not that nobody cares, it's just that it's hard enough to reply to all notifications in the open issues that some of the closed ones pass by (I'm not saying that as an excuse, it's just that it works better to open new ones).

Now, I don't know exactly how this could be solved, so I'm open to suggestions.

tegon avatar Oct 08 '18 18:10 tegon

@benbonnet @tegon @themilkman @marcosanfilippo @alanyeh20001 please have a look on this https://github.com/plataformatec/devise/issues/5017

theshashiverma avatar Jan 31 '19 04:01 theshashiverma

I've the same problem.

I just run rails generate devise:views only.

Custom mailer views is rendered correctly.

But they not render after convert from ERB to HAML.

Environment

  • Ruby 2.6.3
  • Rails 5.2.3
  • Devise 4.6.2

akeuk avatar Jun 04 '19 02:06 akeuk

In my case, I had a similar issue where layout 'my/layout/file/here worked but default template_path: 'my/template/source' did not work. I solved it by setting the config.scoped_views = true in the devise.rb file

bafrimpong avatar Aug 08 '19 09:08 bafrimpong

Please update https://github.com/heartcombo/devise/wiki/How-To:-Use-custom-mailer

This bug still persists. Rails 7.2, devise 4.9.4

Just doing the beloew dont work:

class CustomDeviseMailer < Devise::Mailer
  layout "mailer"
  default template_path: "users/mailer"
end

Or you use this work around:

class CustomDeviseMailer < Devise::Mailer
  layout "mailer"

  def headers_for(action, opts)
    super.merge!(template_path: "users/mailer")
  end
end

OR set this option to true on config/initializers/devise.rb:

config.scoped_views = true

mpancada avatar Aug 24 '24 11:08 mpancada