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.
Tom H. (Guest)
on 2009-04-22 20: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
Sijo k. (Guest)
on 2009-04-23 09:55
Did you try like
include PhonenumberHelper
      in mailings_controller.rb  and
is phonenumber_helper.rb in app/helpers?

Sijo
bushfreakz (Guest)
on 2009-04-23 11: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
Tom H. (Guest)
on 2009-04-23 14: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.