Customize Mailer views for different resource types

959 views
Skip to first unread message

Andion

unread,
Mar 31, 2010, 11:28:38 AM3/31/10
to Devise
Following the readme,I wanted to have customized mail views so I have
different mails for different resources (admin & user in my case) but
had no success:

1 - Using rails 3.0.0beta & devise 1.1.pre4
2 - I've created app/views/admins/mailer/ & app/views/users/mailer/
with my email personalizations > Doesn't work
3 - I've tested that scoped views are working by customizing sign_in
(sessions/new) views > Working

I've read devise code and I figured out (I don't know if correctly)
that the mapping should be to my devise_mapping.as set on
devise_for... but it's aready correct (tested with ActionView scoped
views working)

So...
Devise::Mailer#setup_mail(record, action)
calls:
render_with_scope(action, :controller => "mailer")
and there it renders the template with:
render :template => "#{devise_mapping.as}/#{controller_name}/
#{action}"

... read that everything seems like in normal scoped views to me... so
I don't understand why it's not working: Anybody knows what obvious
point am I missing ?

Thanks!

José Valim

unread,
Mar 31, 2010, 11:34:31 AM3/31/10
to plataforma...@googlegroups.com
Unfortunately it looks like a bug to me. This is definitely fixed on Devise master, so you can either migrate to Devise master or live with the bug until a new Devise gem be released (which will be right after a new Rails beta is released).



--
To unsubscribe, reply using "remove me" as the subject.



--
José Valim

Director of Engineering - Plataforma Tecnologia
Know more about us: http://plataformatec.com.br/en/

Andion

unread,
Mar 31, 2010, 12:25:26 PM3/31/10
to Devise
Checked with devise master (015c74e) & rails master (db28d407) - still
have the exact same problem. This is what makes me think it's my
fault.

Somebody made personalized mail views and has some tips to share? :)

José Valim

unread,
Mar 31, 2010, 12:32:16 PM3/31/10
to plataforma...@googlegroups.com
In devise test suite, there is a Rails application inside test/rails_app/. Maybe it can give you some hints?

Notice that there is a view at test/rails_app/app/views/users/mailer/confirmation_instructions.erb.

Are you using mailer views with mime type like confirmations_instructions.html.erb? Maybe this is the issue?

Andion

unread,
Mar 31, 2010, 12:39:00 PM3/31/10
to Devise
I'm using mime_types, but they work: The problem is the mailer view
rendered is always from the 'user' resource scope.

I'll check the test/rails_app, thanks a lot :)

José Valim

unread,
Mar 31, 2010, 12:53:17 PM3/31/10
to plataforma...@googlegroups.com
So you have both app/views/users/mailer and app/views/admins/mailer?
And it's always picking up the first?

What `Devise.mappings` returns on console? Calling .as on the mappings are returning the proper value?

Andion

unread,
Mar 31, 2010, 1:05:25 PM3/31/10
to Devise
Yes :/

Andion

unread,
Mar 31, 2010, 1:28:40 PM3/31/10
to Devise
I'm out until monday, then I'll check everything again using test/
rails_app to see if it's my code. Taking a look at it the only
difference seems the lack of mime-type on the mail layout. Thank's a
lot

Andion

unread,
Apr 7, 2010, 6:37:15 AM4/7/10
to Devise
I knew it must be my fault!

I'll explain my mistake, in case somebody was interested on this
thread: Views, as we stated here are scoped according using resource
mappings; i.e: if you have an 'user' an 'admin' devise models, then
you'll have a two different mappings more or less like this one:

>> y Devise.mappings[:admin]
--- !ruby/object:Devise::Mapping
as: :admins
controllers:
:sessions: devise/sessions
klass: Admin
modules:
- :database_authenticatable
- :rememberable
- :validatable
- :timeoutable
- :trackable
name: :admin
path_names:
:sign_in: sign_in
:sign_out: sign_out
path_prefix: /
routes:
- :session
strategies:
- :rememberable
- :database_authenticatable
=> nil

So, we can have two possible resource types logged in, and on most
cases, this is what will make devise to render it's scoped view

render :template => "#{devise_mapping.as}/#{controller_name}/"

Then I mixed it on my mind with my personal configuration (users can
sign_up or an admin may register them) and expect that when an admin
where creating the user, /admins/mailer/* views will be used. Stupid
mistake: setup_mail is called from devise model's after_save (when
using confirmable, for example) and the current_user, nor the request
has to do with it: so the mailer view always rendering was the one
from the model (user on this case)

Finally: Solution to my willings? I don't know, either not using
confirmable at all or personalize users mail view according to
current_user. This was what made me go through the wrong way at first,
because you can't use current_user or auth helpers like {resource}
_signed_in? on mail views cause they are apart from the
request :-/. ... any ideas, anybody?

PS: I have an ugly, *ugly* temporal solution but I don't recommend it,
if anyone's interested i'll post it here.

So thanks Jose for all your patience, and sorry for my mistake :-/

Reply all
Reply to author
Forward
0 new messages