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, which seems to work, except the methods defined therein are not available to my mailer (i get undefined errors). Does anyone know how this is supposed to work?
Here's my class:
class CommentMailer < ActionMailer::Base default :from => "Andre Fournier <[email protected]>" helper :application end
Thanks, Sean
A helper is a method that is (mostly) used in your Rails views to share reusable code. Rails comes with a set of built-in helper methods. One of these built-in helpers is time_ago_in_words . This method is helpful whenever you want to display time in this specific format.
A Helper method is used to perform a particular repetitive task common across multiple classes. This keeps us from repeating the same piece of code in different classes again and again. And then in the view code, you call the helper method and pass it to the user as an argument.
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).
helper ApplicationHelper
class NotificationsMailer < ActionMailer::Base default from: "Community Point <[email protected]>" 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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With