Most of the time the helper classes are geared towards generating boilerplate (or almost boilerplate) code for output. I am wanting to use a helper for boilerplate or almost boilerplate processing instead. I have a certain process that needs to happen for several controllers. Instead of just copying the action to all the controllers I figured I would put the action in a Helper module and then simply use the helper class method to mixin that action to my controller. When I tried to do this I get a message about the controller not being able to respond to the requested action. To get around this I manually required the helper file and manually mixed it into my controller module and that worked great. My question is why didn''t simply using the helper class method work? The code looks seems to just require the file and do an include in a class_eval. Basically it is doing the same thing I did manually. So why doesn''t this work? The bug is not that big of a deal, but if I can simply my code I would like to. Anybody have any ideas? Eric _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
David Heinemeier Hansson
2005-Jan-12 21:07 UTC
Re: Using Helper Classes for standard processing
> I have a certain process that needs to happen for several controllers. > Instead of just copying the action to all the controllers I figured I > would put the action in a Helper module and then simply use the helper > class method to mixin that action to my controller. > > When I tried to do this I get a message about the controller not being > able to respond to the requested action. To get around this I manually > required the helper file and manually mixed it into my controller > module and that worked great. > > My question is why didn''t simply using the helper class method work? > The code looks seems to just require the file and do an include in a > class_eval. Basically it is doing the same thing I did manually. So > why doesn''t this work? > > The bug is not that big of a deal, but if I can simply my code I would > like to. Anybody have any ideas?Helpers are not your normal Ruby module mixins. They add methods that''ll only be available to the view. They''re View Helpers, not Controller Helpers. Want you want is instead just to make a regular Ruby module and include it with "include MyModule". Alternatively, you could pull your shared methods up into the ApplicationController. -- David Heinemeier Hansson, http://www.basecamphq.com/ -- Web-based Project Management http://www.rubyonrails.org/ -- Web-application framework for Ruby http://macromates.com/ -- TextMate: Code and markup editor (OS X) http://www.loudthinking.com/ -- Broadcasting Brain
David, I see this a question that will arise often to new users of the framework. It would be nice to have this stated very clearly in the Railties documentation: "Helpers are for views only". I am saying this because I spent a little time figuring this out too with my application. David Heinemeier Hansson wrote:>> I have a certain process that needs to happen for several >> controllers. Instead of just copying the action to all the >> controllers I figured I would put the action in a Helper module and >> then simply use the helper class method to mixin that action to my >> controller. >> >> When I tried to do this I get a message about the controller not >> being able to respond to the requested action. To get around this I >> manually required the helper file and manually mixed it into my >> controller module and that worked great. >> >> My question is why didn''t simply using the helper class method work? >> The code looks seems to just require the file and do an include in a >> class_eval. Basically it is doing the same thing I did manually. So >> why doesn''t this work? >> >> The bug is not that big of a deal, but if I can simply my code I >> would like to. Anybody have any ideas? > > > Helpers are not your normal Ruby module mixins. They add methods > that''ll only be available to the view. They''re View Helpers, not > Controller Helpers. Want you want is instead just to make a regular > Ruby module and include it with "include MyModule". > > Alternatively, you could pull your shared methods up into the > ApplicationController. > -- > David Heinemeier Hansson, > http://www.basecamphq.com/ -- Web-based Project Management > http://www.rubyonrails.org/ -- Web-application framework for Ruby > http://macromates.com/ -- TextMate: Code and markup editor (OS X) > http://www.loudthinking.com/ -- Broadcasting Brain > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails > >
On Thu, 13 Jan 2005 10:07:44 -0300, Demetrius Nunes <demetrius-fDpYTK8McCzCdMRJFJuMdgh0onu2mTI+@public.gmane.org> wrote:> David, I see this a question that will arise often to new users of the > framework. > > It would be nice to have this stated very clearly in the Railties > documentation: "Helpers are for views only". > > I am saying this because I spent a little time figuring this out too > with my application.This is also confusing a bit because in older versions (<=7.x, I think) helper methods were available to controllers. This did throw me for a bit of a loop after an upgrade.
That''s interesting. I hadn''t realized that, and was always wondering what helpers are for. Are there any guidelines anywhere about when helpers should be used? I see helpers as somewhat dangerous, giving lots of temptation to programmers to put a lot of logic in the view/helper that should be in the controller. On Thu, 13 Jan 2005 10:07:44 -0300, Demetrius Nunes <demetrius-fDpYTK8McCzCdMRJFJuMdgh0onu2mTI+@public.gmane.org> wrote:> David, I see this a question that will arise often to new users of the > framework. > > It would be nice to have this stated very clearly in the Railties > documentation: "Helpers are for views only". > > I am saying this because I spent a little time figuring this out too > with my application. > > David Heinemeier Hansson wrote: > > >> I have a certain process that needs to happen for several > >> controllers. Instead of just copying the action to all the > >> controllers I figured I would put the action in a Helper module and > >> then simply use the helper class method to mixin that action to my > >> controller. > >> > >> When I tried to do this I get a message about the controller not > >> being able to respond to the requested action. To get around this I > >> manually required the helper file and manually mixed it into my > >> controller module and that worked great. > >> > >> My question is why didn''t simply using the helper class method work? > >> The code looks seems to just require the file and do an include in a > >> class_eval. Basically it is doing the same thing I did manually. So > >> why doesn''t this work? > >> > >> The bug is not that big of a deal, but if I can simply my code I > >> would like to. Anybody have any ideas? > > > > > > Helpers are not your normal Ruby module mixins. They add methods > > that''ll only be available to the view. They''re View Helpers, not > > Controller Helpers. Want you want is instead just to make a regular > > Ruby module and include it with "include MyModule". > > > > Alternatively, you could pull your shared methods up into the > > ApplicationController. > > -- > > David Heinemeier Hansson, > > http://www.basecamphq.com/ -- Web-based Project Management > > http://www.rubyonrails.org/ -- Web-application framework for Ruby > > http://macromates.com/ -- TextMate: Code and markup editor (OS X) > > http://www.loudthinking.com/ -- Broadcasting Brain > > > > _______________________________________________ > > Rails mailing list > > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > > http://lists.rubyonrails.org/mailman/listinfo/rails > > > > > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
Steve Willer wrote:> That''s interesting. I hadn''t realized that, and was always wondering > what helpers are for. > > Are there any guidelines anywhere about when helpers should be used? I > see helpers as somewhat dangerous, giving lots of temptation to > programmers to put a lot of logic in the view/helper that should be in > the controller.Helpers are view-code (as in the V in MVC) and are intended to keep presentation logic out of the template. Take a look at the helpers included with Action View to get an idea of the sorts of things helpers are intended for - mostly building HTML constructs for you, such as form elements, URLs, etc. As a rough guideline, you use controller code to grab objects from the db, modify their values based on user input, etc ("controlling") and helper code to turn objects into HTML (or XML, or text, or PNG...) suitable for displaying to the user ("viewing"). Modifying an object shouldn''t occur in a helper, but equally you shouldn''t be generating HTML in the controller. The line between the two is slightly blurry, and what code goes where is entirely up to the programmer. Asking the programmer to make design decisions isn''t "dangerous", and Rails makes no attempt to prevent it. On the other hand, the point of helpers is to keep that presentation logic from cluttering up the template, which is bad. Tim. -- Tim Bates tim-kZbwfhiKUx26c6uEtOJ/EA@public.gmane.org
Okay, thanks. FYI, the "dangerous" comment really just comes from my own experience in the past with php/asp/jsp-style templates that allow big blocks of code ... programmers can often end up throwing a lot of code into those blocks if they''re misbehaved or lazy or under the gun. It has ended up causing a lot of problems later, so that''s where the concern comes from. On Fri, 14 Jan 2005 07:08:53 +1030, Tim Bates <tim-kZbwfhiKUx26c6uEtOJ/EA@public.gmane.org> wrote:> Steve Willer wrote: > > That''s interesting. I hadn''t realized that, and was always wondering > > what helpers are for. > > > > Are there any guidelines anywhere about when helpers should be used? I > > see helpers as somewhat dangerous, giving lots of temptation to > > programmers to put a lot of logic in the view/helper that should be in > > the controller. > > Helpers are view-code (as in the V in MVC) and are intended to keep > presentation logic out of the template. Take a look at the helpers > included with Action View to get an idea of the sorts of things helpers > are intended for - mostly building HTML constructs for you, such as form > elements, URLs, etc. > > As a rough guideline, you use controller code to grab objects from the > db, modify their values based on user input, etc ("controlling") and > helper code to turn objects into HTML (or XML, or text, or PNG...) > suitable for displaying to the user ("viewing"). Modifying an object > shouldn''t occur in a helper, but equally you shouldn''t be generating > HTML in the controller. > > The line between the two is slightly blurry, and what code goes where is > entirely up to the programmer. Asking the programmer to make design > decisions isn''t "dangerous", and Rails makes no attempt to prevent it. > On the other hand, the point of helpers is to keep that presentation > logic from cluttering up the template, which is bad. > > Tim. > > -- > Tim Bates > tim-kZbwfhiKUx26c6uEtOJ/EA@public.gmane.org >
On Wed, Jan 12, 2005 at 10:07:11PM +0100, David Heinemeier Hansson wrote:> >I have a certain process that needs to happen for several controllers. > >Instead of just copying the action to all the controllers I figured I > >would put the action in a Helper module and then simply use the helper > >class method to mixin that action to my controller. > > > >When I tried to do this I get a message about the controller not being > >able to respond to the requested action. To get around this I manually > >required the helper file and manually mixed it into my controller > >module and that worked great. > > > >My question is why didn''t simply using the helper class method work? > >The code looks seems to just require the file and do an include in a > >class_eval. Basically it is doing the same thing I did manually. So > >why doesn''t this work? > > > >The bug is not that big of a deal, but if I can simply my code I would > >like to. Anybody have any ideas? > > Helpers are not your normal Ruby module mixins. They add methods > that''ll only be available to the view. They''re View Helpers, not > Controller Helpers. Want you want is instead just to make a regular > Ruby module and include it with "include MyModule". > > Alternatively, you could pull your shared methods up into the > ApplicationController.I sort of feel like I am introduction crack to school children but there is a class method called ''helper_method'' which you can call in your controllers which makes a method defined in your controller available to you views. e.g. class LalaController < ActionController::Base protected def logged_in? # code end helper_method :logged_in? end Use sparingly I suppose. marcel -- Marcel Molina Jr. <marcel-WRrfy3IlpWYdnm+yROfE0A@public.gmane.org>
> I sort of feel like I am introduction crack to school children but there > is a class method called ''helper_method'' which you can call in your > controllers which makes a method defined in your controller available > to you views.Thanks Marcel! I hadn''t seent hat yet. That''s more convienent that mixing in modules to helpers and controllers just so they can share code.