0

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.

jpw
  • 18,697
  • 25
  • 111
  • 187
  • Have you tried to require the helper in your actionmailer? I think this is not done automagically like in the views. – Miquel Feb 20 '11 at 09:14
  • how do I do that? I've never needs ot do that before (kind of a rails newbie). – jpw Feb 20 '11 at 09:15
  • Looks like `pretty_phone` is being called as a class method? If you defined it as a instance method, try defining it as a class method (`self.pretty_phone`) and see if that works? – Zabba Feb 20 '11 at 11:38
  • possible duplicate of [Where do I put helper methods for ActionMailer views?](http://stackoverflow.com/questions/3681607/where-do-i-put-helper-methods-for-actionmailer-views) – edgerunner Apr 25 '15 at 20:26
  • nope not a dupe since the goal (and solution) was to use existing application helper method in both the model and view of actionmalier – jpw Apr 27 '15 at 05:28

2 Answers2

3

Where do I put helper methods for ActionMailer views? i think this is exactly what you need.

Community
  • 1
  • 1
Petya petrov
  • 2,153
  • 5
  • 23
  • 35
  • hmmm. thanks but nope. get exactly the same error. restarted my local server and delayed job task too. put mailer_helper.rb in the models folder (altho i used the definition "module MailerHelper" (not class) since that's required there). And then added "helper MailerHelper" as the first line of the foo_mailer.rb file. Same error. – jpw Feb 20 '11 at 09:49
  • 1
    Why would you put MailerHelper in the models folder and not in the helpers folder? – Andrew Marshall Feb 20 '11 at 10:14
1

the answer is posted as part of the question. thanks to all those whose comments led to finally figuring it out. I'm trying to "mark" this question as answered so people won't feel like they need to supply another answer, but somebody deleted my earlier answer. so I'm adding this back in again so in a day or two the system will let me flag this quesiton as answered.

jpw
  • 18,697
  • 25
  • 111
  • 187