# Share code between view and controller

**URL:** <https://www.ruby-forum.com/t/share-code-between-view-and-controller/143259>\
**Category:** Rails\
**Created:** [July 16, 2008, 8:48pm UTC](https://www.ruby-forum.com/t/share-code-between-view-and-controller/143259 "2008-07-16T20:48:26Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [July 16, 2008, 8:48pm UTC](https://www.ruby-forum.com/t/share-code-between-view-and-controller/143259/1 "2008-07-16T20:48:26Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [July 16, 2008, 9:54pm UTC](https://www.ruby-forum.com/t/share-code-between-view-and-controller/143259/2 "2008-07-16T21:54:02Z")

</div>

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, “removed\_email\_address@domain.invalid”

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [July 17, 2008, 3:35am UTC](https://www.ruby-forum.com/t/share-code-between-view-and-controller/143259/3 "2008-07-17T03:35:45Z")

</div>

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, “removed\_email\_address@domain.invalid”

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
