I have a function i want to be used in both views and controllers: def year_to_fiscal_year(year) # converts, say, an integer 2003 into ''0304'' year.to_s[2,2] << (year+1).to_s[2,2] end Where should i put it ? -- Posted via http://www.ruby-forum.com/.
Chris, I just had a similar scenario. Put that code in your app/controller/application.rb Then, also in application.rb, add "helper_method :year_to_fiscal_year" (That exposes it to your Views as well.) Hope that helps, Regards, EJC On 12/19/05, Chris Hulbert <chris.hulbert-gR5rH3iIA/qLnZed9OPc8Q@public.gmane.org> wrote:> I have a function i want to be used in both views and controllers: > > def year_to_fiscal_year(year) > # converts, say, an integer 2003 into ''0304'' > year.to_s[2,2] << (year+1).to_s[2,2] > end > > Where should i put it ? > > -- > Posted via http://www.ruby-forum.com/. > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
Chris > I have a function i want to be used in both views and controllers: If you try and place it in application_helper.rb , controllers won''t have access to it => you''ll get an error message. => you must place it in ApplicationController. Alain -- Posted via http://www.ruby-forum.com/.
Chris Hulbert
2005-Dec-20 03:57 UTC
Re: Where to put code - app controller, or app helper?
Ed C. wrote:> Chris, > > I just had a similar scenario. > > Put that code in your app/controller/application.rb > > Then, also in application.rb, add "helper_method :year_to_fiscal_year" > > (That exposes it to your Views as well.) > > Hope that helps, > Regards, > EJCWorked a treat, thanks! -- Posted via http://www.ruby-forum.com/.