SOLVED. See note 2 and 3.
I guess I just don't understand classes in rails 3 + ruby 1.9.2...
We're using actionmailer in conjunction with delayedjob. It all works fine EXCEPT I'm just trying to pretty-up some phone numbers and I'm trying to put a simple method pretty_phone (to format a phone number) SOMEWHERE that doesn't throw the error:
Class#sample_email failed with NoMethodError: undefined method `pretty_phone'
I've tried it in model_helper.rb, application_helper.rb, and in the class for what I guess is the model for our email foo_mailer.rb (FooMailer < ActionMailer::Base)
Our project setup is this:
app
controllers
widgets_controller.rb
helpers
application_helper.rb
widgets_helper.rb
mailer
foo_mailer.rb ***
models
widget.rb
views
widget
(usual edit,show, etc views)
foo_mailer
sample_email.html.haml ***
This is the simple method I'm trying to add:
# num is a string, always in format 12223334444 we want (222)333-4444
def pretty_phone(num)
return "(" + num[1,3] + ")" + num[4,3] + "-" + num[7,4]
end
foo_mailer.rb is very simple:
class FooMailer < ActionMailer::Base
helper :application **** THIS ALMOST MAKES IT WORK SEE NOTE 2
include ApplicationHelper **** AND THIS ALSO IS REQUIRED NOTE 3
default :from => "Support <support@mydomain.com>"
def sample_email(thewidget)
@widget = thewidget
send_to = @widget.contact_email
if !send_to.blank?
mail :to => send_to,
:subject => "alert regarding #{pretty_phone(@widget.userphone)}"
end
end
end
and down in our view for the email we also use #{pretty_phone(@widget.userphone)}
I'd really like to understand why I can't put that helper in application_helper or even foo_mailer.rb and have it work -- and where it SHOULD go?
(Currently I have the helper in application_help.rb and ALL of our widget erb views can use it fine... it's just the email view and/or the class file foo_mailer.rb that throw the error.)
NOTE2
by adding helper :application
at the top of foo_mailer.rb NOW the pretty_phone() helper method in application_help.rb works in the foo_mailer VIEWs but NOT in the foo_mailer.rb itself. So for example where I want to to pretty_phone() in the subject line of the email it will not work. But in the actual emails (the views) it does work.
That just seems bizarre to me - any suggestions?
NOTE 3 Adding the 'include' AND the 'helper' is what was needed.