gerry.jenkins-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
2008-Jul-16 18:47 UTC
share code between view and controller
I am not using 2.1 Rails yet. But I need to define a common function to be used by both views and controllers. it is a formatting function to be used to format output for logger in controllers and by views. I am having trouble finding a place to define this function to be accessed by both. What is he best approach? --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
You could put the formatting function in your Application controller, then use the helper_method to give the view access. The code below will allow you to use ''my_formatting_method'' in a view. class ApplicationController < ActionController::Base protected def my_formatting_method .... end self.send :helper_method, :my_formatting_method end On Jul 16, 11:47 am, "gerry.jenk...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org" <gerry.jenk...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> I am not using 2.1 Rails yet. > > But I need to define a common function to be used by both views and > controllers. > > it is a formatting function to be used to format output for logger in > controllers and by views. > > I am having trouble finding a place to define this function to be > accessed by both. > > What is he best approach?--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
stephanie wrote:> You could put the formatting function in your Application controller, > then use the helper_method to give the view access. The code below > will allow you to use ''my_formatting_method'' in a view. > > class ApplicationController < ActionController::Base > > protected > def my_formatting_method > .... > end > > self.send :helper_method, :my_formatting_method > end > > > On Jul 16, 11:47�am, "gerry.jenk...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org"Depending on how many methods (or the nature of them) you want to share, you can also put them in a module and include the module in a helper. lib/my_cool_methods.rb module MyCoolMethods def cool_method_one .... end .... end config/environment.rb (at the bottom) require ''my_cool_methods'' app/helpers/my_cool_helper.rb module MyCoolHelper include MyCoolMethods end requiring the module in environment.rb makes it available all over the application, so you don''t have to require it in every file you might want to use it in. It immediately becomes available to all of your controllers. Mixing the module into the helper makes all of the modules methods available in the helper, and since you generally have the "helper :all" statement in application.rb, the helper will get picked up automatically and it will be available to all of your views. I was struggling to share some methods a few weeks ago, and came upon this by trial and error. Peace, Phillip -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---