Add helpers to Devise Mailer

3,327 views
Skip to first unread message

Jared Mehle

unread,
Jul 1, 2010, 2:58:50 PM7/1/10
to Devise
How would I go about adding some custom helper methods to only the
Devise mailer. In a standard Rails mailer, you can write:

helper :my_helper

to load a custom helper module for use in the mailer. I've tried
opening up the Devise::Mailer class and adding that, but it doesn't
work. This is likely due to the Devise modules being autoloaded and
not being available at the time I try to insert my helpers.

Jared Mehle

unread,
Jul 12, 2010, 12:05:16 PM7/12/10
to Devise
I solved my own problem. You need to crack open DeviseMailer (not
Devise::Mailer) to add helpers.

Like so:
DeviseMailer.class_eval do
helper :mailer
end


On Jul 1, 1:58 pm, Jared Mehle <jrme...@gmail.com> wrote:
> How would I go about adding some custom helper methods to only the
> Devisemailer. In a standard Railsmailer, you can write:
>
> helper :my_helper
>
> to load a custom helper module for use in themailer. I've tried
> opening up the Devise::Mailerclass and adding that, but it doesn't

docgecko

unread,
Mar 18, 2011, 10:36:41 AM3/18/11
to plataforma...@googlegroups.com
Hey Jared,

I have tried to change my application.rb file from:

<pre>config.to_prepare do
   Devise::Mailer.class_eval do 
      helper :url 
   end
end</pre>

to:

<pre>config.to_prepare do
   Devise::Mailer.class_eval do 
      helper :url 
   end
end</pre>

and I now get the following error when I try to start my server.

<pre>`const_missing': uninitialized constant TestApp::Application::DeviseMailer</pre>

To get this to work, were you also placing your DeviseMailer code in the config/application.rb file or somewhere else?

Cheers,


D.

Mike Jarema

unread,
Mar 1, 2013, 12:09:07 PM3/1/13
to plataforma...@googlegroups.com
To resolve this issue I've placed the class_eval code at the bottom of my Devise initializer, specifically after the Devise.setup block.

# config/initializers/devise.rb

Devise.setup do |config|
  # ... existing Devise configuration, no modification necessary to include helper(s) ...
end

Devise::Mailer.class_eval do
  helper :mailer # include "MailerHelper", adjust to suit your needs
end

Hope that helps anyone who Googles this.

- Mike

Christian Gambardella

unread,
Nov 9, 2013, 7:20:18 AM11/9/13
to plataforma...@googlegroups.com
works like a charm! thx!

Brett Lischalk

unread,
Jan 7, 2014, 1:17:03 PM1/7/14
to plataforma...@googlegroups.com
I actually had some issues with this class_eval code in development.  It will work for a while and then my helper would stop being found as I am doing development.  I believe the issue may stem from using class_eval instead of instance_eval.

When I started using instance_eval:

Devise::Mailer.instance_eval do
  helper :mailer # include "MailerHelper", adjust to suit your needs
end

My helper would be found consistently.  I think the problem is the class method "helper" needs to be called in the context of the "class object instance"

- Brett

Michail Karavaev

unread,
Jan 9, 2016, 8:07:40 AM1/9/16
to Devise
I had same issues with instance_eval.
Hope this will help someone.

add this to application.rb
   
 config.to_prepare do
      Devise::Mailer.helper :my_helper
  end
Reply all
Reply to author
Forward
0 new messages