Forum: Ruby on Rails Sharing function between helper and controller

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.
Peter M. (Guest)
on 2006-05-03 20:59
(Received via mailing list)
Hi,

I have a utility function repeated in both my helper and my
controller. Not DRY. I have this repetition  because other functions
in the helper and in the controller both use the utility function.
Where do I put this function so that both the helper and controller
can access the function? Mayber there is a different solution?

Thanks,
Peter
Alain R. (Guest)
on 2006-05-03 21:00
(Received via mailing list)
Peter

    > Where do I put this function so that both the helper and
controller
    > can access the function? Mayber there is a different solution?

helper_method() is what you need.


In the controller: (ex: application.rb)

     helper_method :tx_, :dev?
     def dev?
         ENV['RAILS_ENV']=='development'
     end

=> you can also use it in views.

Alain
This topic is locked and can not be replied to.