Forum: Ruby on Rails Using helper method of another model

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.
r00by n00by (Guest)
on 2006-04-06 21:22
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?
Chris T (Guest)
on 2006-04-06 22:26
(Received via mailing list)
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
r00by n00by (Guest)
on 2006-04-07 01:07
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.
Chris T (Guest)
on 2006-04-07 02:04
(Received via mailing list)
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
simen (Guest)
on 2006-04-07 02:07
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.
r00by n00by (Guest)
on 2006-04-07 02:35
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.
This topic is locked and can not be replied to.