I have trying to access helper methods from a rails 3 mailer in order to access the current user for the session.
I put the helper :application in my mailer class,
To enable you to access application helpers from the ActionMailer views, try adding this:
add_template_helper(ApplicationHelper)
To your ActionMailer (just under your default :from
line).
Josh Pinter's answer is correct, but I discovered that it is not necessary.
What is necessary is to name the helper correctly.
NotificationMailerHelper
is correct. NotificationMailersHelper
(note the s) is not correct.
The class and filename of the helper must match and be correctly spelled.
Rails 3.2.2
helper ApplicationHelper
class NotificationsMailer < ActionMailer::Base
default from: "Community Point <Team@CommunityPoint.ca>"
helper ApplicationHelper
helper NotificationMailerHelper
# ...other code...
NOTE: These helper methods are only available to the Views. They are not available in the mailer class (NotificationMailer
in my example).
If you need them in the actual mailer class, use include ApplicationHelper
, like so:
class NotificationMailer < ActionMailer::Base
include ApplicationHelper
# ... the rest of your mailer class.
end
From this other SO question.
include ActionView::Helpers::TextHelper
worked for me in the Mailer controller (.rb file). This allowed me to use the pluralize helper in a Mailer controller action (helpers worked fine from the get go in Mailer views). None of the other answers worked, at least not on Rails 4.2
A hackish means of achieving what I wanted is to store the objects I need (current_user.name + current_user.email) in thread attributes, like so: Thread.current[:name] = current_user.name
. Then in my mailer I just assigned new instance variables to those values stored in the thread: @name = Thread.current[:name]
. This works, but it won't work if using something like delayed job.