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 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

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...@gmail.com"

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