Where to put code - app controller, or app helper?


#1

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 ?


#2

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


#3

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


#4

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!