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!


Phlip

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


Andrew S.

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

Hi

On Thu, 2008-04-24 at 19:04 -0700, J Y wrote:

the application helper just seems to make it really cluttered, and
True MVC ain’t like that!


you’re obviously not tracking what Phlip is saying here.

Rails v3 is not going to abandon MVC

You need to figure out which code is Model specific and which is
universal and it becomes obvious where the code goes.

It’s been working well for Rails developers and it’s not gonna change.

Craig

Hi –

On Thu, 24 Apr 2008, J Y wrote:

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


Rails training from David A. Black and Ruby Power and Light:
INTRO TO RAILS June 9-12 Berlin
ADVANCING WITH RAILS June 16-19 Berlin
INTRO TO RAILS June 24-27 London (Skills Matter)
See http://www.rubypal.com for details and updates!

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

On Thu, 24 Apr 2008, David A. Black wrote:


Rails training from David A. Black and Ruby Power and Light:
INTRO TO RAILS June 9-12 Berlin
ADVANCING WITH RAILS June 16-19 Berlin
INTRO TO RAILS June 24-27 London (Skills Matter)
See http://www.rubypal.com for details and updates!


Rails training from David A. Black and Ruby Power and Light:
INTRO TO RAILS June 9-12 Berlin
ADVANCING WITH RAILS June 16-19 Berlin
INTRO TO RAILS June 24-27 London (Skills Matter)
See http://www.rubypal.com for details and updates!

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

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 S. 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…


Phlip

Hi –

On Thu, 24 Apr 2008, Phlip wrote:

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.

I think the “helper” method solved J Y’s problem, but it sounds like
you’re talking about a different limitation, involving partials. I’m
afraid I can’t quite work out what you mean, though (in spite of my
being a LZ fan of long standing :slight_smile: What exactly is it you can’t do,
as between a controller and partials in another view directory?

David


Rails training from David A. Black and Ruby Power and Light:
INTRO TO RAILS June 9-12 Berlin
ADVANCING WITH RAILS June 16-19 Berlin
INTRO TO RAILS June 24-27 London (Skills Matter)
See http://www.rubypal.com for details and updates!

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!

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs