devise
devise copied to clipboard
Unable to use custom views for emails
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
Is the above code okay/acceptable ? Did I miss something from the doc ?
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 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
Same here (rails 4.2), thank you for your headers_for-workaround @benbonnet !
@tegon is that an acceptable setup ?
@benbonnet I haven't got the change to look at it yet, sorry. I'll do it when I have some time.
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...
@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 👍
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.
default has no effect is a strange thing, which means custom_path is a useless setting. Should this be considered a bug?
It is a bug, but seems like that unless the bug affects many people, no one cares.
@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.
@benbonnet @tegon @themilkman @marcosanfilippo @alanyeh20001 please have a look on this https://github.com/plataformatec/devise/issues/5017
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
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
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