How to reference view helpers from another controller?

Hi,

I've googled around looking for answers to this simple question, with little success.

How do I go about using a view helper from another controller, without putting the helper into application_helper.rb? For example, I would like to render a partial from the Products controller, when I am in a Manufacturer view. I'd like to keep the helper function in products_helper, but haven't found a way to. Putting everything into the application helper just seems to make it really cluttered, and just about everything ends up there, eventually.

Any help much appreciated!

-j

J Y wrote:

How do I go about using a view helper from another controller, without putting the helper into application_helper.rb? For example, I would like to render a partial from the Products controller, when I am in a Manufacturer view. I'd like to keep the helper function in products_helper, but haven't found a way to. Putting everything into the application helper just seems to make it really cluttered, and just about everything ends up there, eventually.

Put it in app-helper.

If you got clutter, make certain you move as much as you can to the Models!

But a big issue with Rails is you can't plug-and-play controllers with their partials. If a partial gets shared into another controller's view - boom - the shared code has to go up that ol' stairway to heaven.

True MVC ain't like that!

I’d like to keep the helper function in

products_helper, but haven’t found a way to. Putting everything into

the application helper just seems to make it really cluttered, and

just about everything ends up there, eventually.

Create a module in the lib dir, require that module in your Manufacturers and Products helpers and then include the module in those helpers.

e.g. create lib/generic_helper_methods.rb which may look like:

module GenericHelperMethods def some_helper_method end end

now in products_helper.rb

requre “generic_helper_methods”

module ProductsHelper include GenericHelperMethods

now some_helper_method is available

end

Gah, that's what I was afraid of... What a flaw... perhaps there's some hope of getting this changed in v3?

J Y wrote:

Gah, that's what I was afraid of... What a flaw... perhaps there's some hope of getting this changed in v3?

If you mean Rails 2, no, because as Andrew Stone pointed out you still have the full power of Ruby's native class systems at your fingertip.

I would still go with pushing things into Models first, though...

Hi

Hi --

Hi,

I've googled around looking for answers to this simple question, with little success.

How do I go about using a view helper from another controller, without putting the helper into application_helper.rb? For example, I would like to render a partial from the Products controller, when I am in a Manufacturer view. I'd like to keep the helper function in products_helper, but haven't found a way to. Putting everything into the application helper just seems to make it really cluttered, and just about everything ends up there, eventually.

I'm not sure what's not working exactly, which probably means I'm misunderstanding the setup. Is this correct?

   The action is in the manufacturer controller.    The view template is in views/manufacturer.    The view renders a partial from views/products.    That partial calls a method from products_helper.rb.

I'd expect that to work fine. Am I describing it wrongly?

David

Hi --

I should add: make sure that you've got:

   helpers :all

in application.rb. It should be there by default in a Rails 2.0 app.

David

Since you can use the helper macro in your controller (there from the early days), neither is Rails, Philp.

I see... Sorry for my ignorance - I wasn't familiar with the 'helper' keyword, although I had investigated 'include' and 'require'. This is exactly what I needed to know. Thanks!

Hi --