Forum: Ruby on Rails Where to put code - app controller, or app helper?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
B9f2cd54b3f85455af152162c2e5b1b2?d=identicon&s=25 Chris Hulbert (Guest)
on 2005-12-20 00:56
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 ?
648805ae18e92b7c71e46f3caa23ddc8?d=identicon&s=25 Ed C. (Guest)
on 2005-12-20 01:50
(Received via mailing list)
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
B45eab4f18aa1bb2a44d6e657531a642?d=identicon&s=25 Alain Ravet (aravet)
on 2005-12-20 02:06
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
B9f2cd54b3f85455af152162c2e5b1b2?d=identicon&s=25 Chris Hulbert (Guest)
on 2005-12-20 04:57
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,
> EJC

Worked a treat, thanks!
This topic is locked and can not be replied to.