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 ?
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 ?
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
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
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 forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.
Sponsor our Newsletter | Privacy Policy | Terms of Service | Remote Ruby Jobs