Forum: Ruby on Rails How to include a helper method in a controller

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
460ead051b49c0b2a8e45e46252d4bda?d=identicon&s=25 Tom Ha (tom)
on 2009-04-22 18:04
Hi there,

why do I get the error...

undefined method `format_phone_number_for' for
#<User::MailingsController:0xb70aae58>

... in this situation:

controllers/user/mailings_controller.rb:
========================================

class User::MailingsController < ApplicationController
  def whatever
    [...]
    [...] format_phone_number_for(@somebody)     # <- why is this
"undefined" ?
    [...]
  end
end


helpers/phonenumber_helper.rb:
==============================

module PhonenumberHelper
  def format_phone_number_for(parent)
    [...]
  end
end


And what do I have to do to have this method available in this
controller?
(I have already tried by including "helper PhonenumberHelper" in the
controller)

Thanks for your help!
Tom
661ae6e97d0e16c2af2aaf7d197e8387?d=identicon&s=25 Sijo k g (sijo)
on 2009-04-23 07:55
Did you try like
include PhonenumberHelper
      in mailings_controller.rb  and
is phonenumber_helper.rb in app/helpers?

Sijo
3203482ad7f9e102a12ff664b9b5317d?d=identicon&s=25 bushfreakz (Guest)
on 2009-04-23 09:08
(Received via mailing list)
hello im very new to rails but i "think"
it should go in the mailings_helper instead of the phonenumber_helper

or there should be an application_helper   which i think is universal
460ead051b49c0b2a8e45e46252d4bda?d=identicon&s=25 Tom Ha (tom)
on 2009-04-23 12:04
Thanks for your replies!

@ Sijo:
The problem was resolved by adding "include PhonenumberHelper" in the
controller, as you have suggested (I was sure I had done that before...)

@ bushfreakz:
Any helpers in app/helpers are only available to views, by default. If
you have "helper :all" in ApplicationController, all helpers in
app/helpers are available to all *views* only.
This topic is locked and can not be replied to.