rubyonrails-c4f1mTqwXZkdnm+yROfE0A@public.gmane.org
2004-Nov-02 16:06 UTC
Where to put methods used by views
Hi Probably a dumb question, but where do I add a method so that I can access it from within a view? For example --- somefile.rhtml <html> <%= body_method %> </html> --- Where would I define #body_method? Thanks -- Jim Freeze
rubyonrails-c4f1mTqwXZkdnm+yROfE0A@public.gmane.org wrote:> Hi > > Probably a dumb question, but where do I add a method so that > I can access it from within a view? > > For example > > --- somefile.rhtml > > <html> > <%= body_method %> > </html> > > --- > > Where would I define #body_method?In app/helpers/*.rb. The new_controller script autogenerates a <controllername>.rb file which is automatically required & included in the controller, so if #body_method is only for a specific controller, you would put it there. In other cases it might be more apropriate to create a new .rb file there and do the require & include yourself. -- Marten Veldthuis
Marten Veldthuis wrote:>> Probably a dumb question, but where do I add a method so that >> I can access it from within a view? > > > In app/helpers/*.rb.That''s a good choice if it is used by one view, or views that belong to the same controller. Method that is used by multiple views from multiple controllers belongs to abstract_helper.rb. Of course, in both cases you can extract it to a separate file, if it''s worth it. In which case, the file''s place is either in app/helpers or in lib. Alex
So how do you determine what goes in the controller vs. the helper? Only view methods in the controllers? Todd On Tue, 02 Nov 2004 18:59:27 +0200, Alexey Verkhovsky <alex-vV7tgcE2N9Nhl2p70BpVqQ@public.gmane.org> wrote:> Marten Veldthuis wrote: > > >> Probably a dumb question, but where do I add a method so that > >> I can access it from within a view? > > > > > > In app/helpers/*.rb. > > That''s a good choice if it is used by one view, or views that belong to > the same controller. > Method that is used by multiple views from multiple controllers belongs > to abstract_helper.rb. > > Of course, in both cases you can extract it to a separate file, if it''s > worth it. In which case, the file''s place is either in app/helpers or in > lib. > > Alex > > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
I personally don''t put anything the controller-specific helpers (yet). But that''s just me. In the application helper though, I place global formatting things such as "how to display long dates, short dates, date ranges and such". I''ve posted an example on CodePaste. http://codepaste.org/view/paste/448 HTH, Steve On Tue, 2 Nov 2004 11:03:43 -0600, Todd Breiholz <talanb-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> So how do you determine what goes in the controller vs. the helper? > Only view methods in the controllers? > > Todd > > > > > On Tue, 02 Nov 2004 18:59:27 +0200, Alexey Verkhovsky <alex-vV7tgcE2N9Nhl2p70BpVqQ@public.gmane.org> wrote: > > Marten Veldthuis wrote: > > > > >> Probably a dumb question, but where do I add a method so that > > >> I can access it from within a view? > > > > > > > > > In app/helpers/*.rb. > > > > That''s a good choice if it is used by one view, or views that belong to > > the same controller. > > Method that is used by multiple views from multiple controllers belongs > > to abstract_helper.rb. > > > > Of course, in both cases you can extract it to a separate file, if it''s > > worth it. In which case, the file''s place is either in app/helpers or in > > lib. > > > > Alex > > > > > > _______________________________________________ > > 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 >
Todd Breiholz wrote:>So how do you determine what goes in the controller vs. the helper? >Only view methods in the controllers? > > >Controllers call operations on model, and put the data together for views. Views render that data (i.e., convert it to HTML). Helpers help the views to do the rendering. This is the idea, basically. Of course, there is no hard and fast rule about layer separation. When you start writing MVC layers, code tends to gravitate towards controller, because controller is the place that deals in both the model domain and presentation details. Then you look at it and say "aha! this loop here is a model method - it doesn''t need to know anything about the nature of the frontend, or that there is a frontend at all for that matter. And this convoluted gsub there is a view helper method - its output goes straight to the HTML". Alex