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.
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.
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
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!
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!
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 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.