Controller can't find helper?!?


#1

I have a controller named xyzController under app/controllers

I have a helper class named xyzHelper under app/helpers.

Shouldn’t I be able to call methods from xyzHelper inside of
xyzController without any problems?

Thanks,
Wes


#2

From Agile Rails: "If a controller is named BlogController, it will
automatically look for a helper MODULE called BlogHelper in the file
blog_helper.rb in the app/helpers directory.

So the answer is yes, IF: you have a helper MODULE called ZyzHelper in
the file xyz_helper.rb in the app/helpers directory.

Michael


#3

Here’s my module:

module ESimplyHelper
def get_protocol_and_server(request)
request.protocol() + request.host_with_port()
end
end

I am trying to call get_protocol_and_server from ESimplyController.

Is there something wrong with my module?

Thanks,
Wes

Michael T. wrote:

From Agile Rails: "If a controller is named BlogController, it will
automatically look for a helper MODULE called BlogHelper in the file
blog_helper.rb in the app/helpers directory.

So the answer is yes, IF: you have a helper MODULE called ZyzHelper in
the file xyz_helper.rb in the app/helpers directory.

Michael


#4

Philip,

That’s what I thought.

I think it’s confusing that you specify in the controller which helper
the “view” gets to use when you use the “helper” keyword.

Seems like I should be able to “include” my helper and it would work,
although that would be confusing in it’s own way.

Thanks,
Wes

Philip R. wrote:

Wes G. wrote:

I have a controller named xyzController under app/controllers

I have a helper class named xyzHelper under app/helpers.

Shouldn’t I be able to call methods from xyzHelper inside of
xyzController without any problems?

No. Helpers define methods that can be used from the view, not the
controller.


Philip R.
http://tzinfo.rubyforge.org/ – DST-aware timezone library for Ruby


#5

Wes G. wrote:

I have a controller named xyzController under app/controllers

I have a helper class named xyzHelper under app/helpers.

Shouldn’t I be able to call methods from xyzHelper inside of
xyzController without any problems?

No. Helpers define methods that can be used from the view, not the
controller.


Philip R.
http://tzinfo.rubyforge.org/ – DST-aware timezone library for Ruby


#6

On 24/04/2006, at 23:33, Philip R. wrote:

Shouldn’t I be able to call methods from xyzHelper inside of
xyzController without any problems?

No. Helpers define methods that can be used from the view, not the
controller.

I think you’re wrong, Phillip. I’m using my helper code in the
controller, but since helper is a module, you must address it as a
module.
Example code from my controller:

def index
@my_controller_variable =
MyControllerHelper::AClassInsideMyHelper.method(parameters)

I use helpers to store some constants and objects, that are used both
by controller and view. All of it is view-related, but controllers
set or get something from those helpers.

RafaÅ? Komorowski
removed_email_address@domain.invalid
GG: 4083718
http://homepage.mac.com/komor/iblog/


#7

Rafal,

How do I call a function in my module if there is no class in my module

Module x
def y

end
end

What do I need to do in my controller in order to:

  1. Get access to module X
  2. Invoke method y?

I keep getting errors like:

undefined method `validate_and_format_url’ for ESimplyUtil:Module

Thanks,
Wes

RafaÅ? Komorowski wrote:

On 24/04/2006, at 23:33, Philip R. wrote:

Shouldn’t I be able to call methods from xyzHelper inside of
xyzController without any problems?

No. Helpers define methods that can be used from the view, not the
controller.

I think you’re wrong, Phillip. I’m using my helper code in the
controller, but since helper is a module, you must address it as a
module.
Example code from my controller:

def index
@my_controller_variable =
MyControllerHelper::AClassInsideMyHelper.method(parameters)

I use helpers to store some constants and objects, that are used both
by controller and view. All of it is view-related, but controllers
set or get something from those helpers.

RafaÅ? Komorowski
removed_email_address@domain.invalid
GG: 4083718
http://homepage.mac.com/komor/iblog/


#8

On 25/04/2006, at 01:31, Wes G. wrote:

How do I call a function in my module if there is no class in my
module

Sure, you can. Here’s an example from the Ruby-book:

module:

module Trig
PI = 3.141592654
def Trig.sin(x)
puts “sinus #{x}”
end
def Trig.cos(x)
puts “cosinus #{x}”
end
end

how to call a function from module:

Trig.sin(1.1)
Trig.cos(Trig::PI)

So just change your code to:

module ESimplyHelper
  def ESimplyHelper.get_protocol_and_server(request)
    request.protocol() + request.host_with_port()
  end
end

and perhaps you can use it in your controller just like in the Ruby-
book:
ESimplyHelper.get_protocol_and_server(request)

or as in Rails way:
ESimplyHelper::ESimplyHelper.get_protocol_and_server(request)

Give it a try.

RafaÅ? Komorowski
removed_email_address@domain.invalid
GG: 4083718
http://homepage.mac.com/komor/iblog/