Using helper method of another model


#1

I have a template that belongs to a certain model (model A). I need to
call a helper method of a different model (model B). Then I need the
helper method of B to call a helper method of model C.
I tried using “include” and “require” and using namespaces, but I got
“undefined method”. What is the way to do it?


#2

r00by n00by wrote:

I have a template that belongs to a certain model (model A). I need to
call a helper method of a different model (model B). Then I need the
helper method of B to call a helper method of model C.
I tried using “include” and “require” and using namespaces, but I got
“undefined method”. What is the way to do it?

Disclaimer: a newb writes:
Try helper :modelB in your A controller to allow B’s helper to be
accessible in A. Not sure bout the nesting, but might work.
Alternatively, i think if you put the helper methods in
application_helper.rb they should be able to see each other.
Hope this helps
Chris T


#3

Chris T wrote:

r00by n00by wrote:

I have a template that belongs to a certain model (model A). I need to
call a helper method of a different model (model B). Then I need the
helper method of B to call a helper method of model C.
I tried using “include” and “require” and using namespaces, but I got
“undefined method”. What is the way to do it?

Disclaimer: a newb writes:
Try helper :modelB in your A controller to allow B’s helper to be
accessible in A. Not sure bout the nesting, but might work.
Alternatively, i think if you put the helper methods in
application_helper.rb they should be able to see each other.
Hope this helps
Chris T

I tried “helper :modelB” and it did help a little. I can now call
BHelper.foo from a template that belongs to A , but now I have
“undefined method” when I try to call link_to inside BHelper.foo .
AHelper also has a method called “foo” so I can’t drop the namespace.


#4

r00by n00by wrote:

I have a template that belongs to a certain model (model A). I need to
call a helper method of a different model (model B). Then I need the
helper method of B to call a helper method of model C.
I tried using “include” and “require” and using namespaces, but I got
“undefined method”. What is the way to do it?

Put the methods you need across models in the ApplicationHelper.


#5

These are good solutions and maybe I will use them, but I would still
like to know how to call link_to from BHelper.foo (using namespace when
defining the method). I thought ActionView::Helpers::UrlHelper::link_to
should work. It didn’t.


#6

r00by n00by wrote:

I tried “helper :modelB” and it did help a little. I can now call
BHelper.foo from a template that belongs to A , but now I have
“undefined method” when I try to call link_to inside BHelper.foo .
AHelper also has a method called “foo” so I can’t drop the namespace.

Not sure, but can you refer to fully qualified names ModelA :: foo
(sorry don’t have Agile book to hand at the moment). Alternatively,
maybe you will just have to rename some of the methods afoo, bfoo etc.
Sorry can’t be more of a help at the moment.
Chris T