# QUICK QUESTION: Method in Controller AND Helper

**URL:** <https://www.ruby-forum.com/t/quick-question-method-in-controller-and-helper/107261>\
**Category:** Rails\
**Created:** [June 19, 2007, 8:14pm UTC](https://www.ruby-forum.com/t/quick-question-method-in-controller-and-helper/107261 "2007-06-19T20:14:33Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![joep](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/87869e/32.png) [@joep](https://www.ruby-forum.com/u/joep)\
**Post date:** [June 19, 2007, 8:14pm UTC](https://www.ruby-forum.com/t/quick-question-method-in-controller-and-helper/107261/1 "2007-06-19T20:14:33Z")

</div>

Hey,

Is there a way to write a method in the application controller and also  
have it available as a helper method without rewriting it in the  
application helper?

I’m pretty sure there is, but haven’t found the specifics on how to do  
this yet.

Joe

---

<div class="post-metadata">

**Author:** ![joep](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/87869e/32.png) [@joep](https://www.ruby-forum.com/u/joep)\
**Post date:** [June 19, 2007, 8:37pm UTC](https://www.ruby-forum.com/t/quick-question-method-in-controller-and-helper/107261/2 "2007-06-19T20:37:33Z")

</div>

I use this all the time;

def current\_user  
@current\_user ||= session[:user] ? User.find\_by\_id(session[:user]) :  
nil  
end  
helper\_method :current\_user

Gokhan  
[www.sylow.net](http://www.sylow.net)

Joe P. wrote:

> Hey,
> 
> Is there a way to write a method in the application controller and also  
> have it available as a helper method without rewriting it in the  
> application helper?
> 
> I’m pretty sure there is, but haven’t found the specifics on how to do  
> this yet.
> 
> Joe

---

<div class="post-metadata">

**Author:** ![joep](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/87869e/32.png) [@joep](https://www.ruby-forum.com/u/joep)\
**Post date:** [June 19, 2007, 8:55pm UTC](https://www.ruby-forum.com/t/quick-question-method-in-controller-and-helper/107261/3 "2007-06-19T20:55:03Z")

</div>

> def current\_user  
> @current\_user ||= session[:user] ? User.find\_by\_id(session[:user]) :  
> nil  
> end  
> helper\_method :current\_user  
> Thanks, now THAT’S what I’m talking about.

---

<div class="post-metadata">

**Author:** ![joep](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/87869e/32.png) [@joep](https://www.ruby-forum.com/u/joep)\
**Post date:** [July 15, 2007, 5:33am UTC](https://www.ruby-forum.com/t/quick-question-method-in-controller-and-helper/107261/4 "2007-07-15T05:33:56Z")

</div>

Is it possible to have method(s) in application\_helper.rb available  
for controllers, just like for views?  
Actually my method is current\_user too 😃 and looks pretty similar,  
but I’d like it in application\_helper.rb…

---

<div class="post-metadata">

**Author:** ![joep](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/87869e/32.png) [@joep](https://www.ruby-forum.com/u/joep)\
**Post date:** [July 15, 2007, 11:55am UTC](https://www.ruby-forum.com/t/quick-question-method-in-controller-and-helper/107261/5 "2007-07-15T11:55:28Z")

</div>

James Schementi schrieb:

> def method\_to\_use\_in\_controller  
> gets included into both the helper and controller.

Hi James,

instead of mixin in a whole helper module I’d declare the Helper method  
as module\_function like:

module SomeHelper  
1000.times do |index|  
define\_method(:“waste\_#{index}”) { index \*\* 2 }  
end  
def some\_method(\*some\_args)  
some\_args.inspect  
end

module\_function :some\_method  
end

In your controller you can then do:

class Somes \< ApplicationController  
def some\_action  
SomeHelper.some\_method  
end  
end

Don’t pollute your controller… 😉

Regards  
Florian

---

<div class="post-metadata">

**Author:** ![joep](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/87869e/32.png) [@joep](https://www.ruby-forum.com/u/joep)\
**Post date:** [July 15, 2007, 6:04pm UTC](https://www.ruby-forum.com/t/quick-question-method-in-controller-and-helper/107261/6 "2007-07-15T18:04:49Z")

</div>

oh wow.  
“module\_function”

i’ve seen it, but never paid any attention.  
thanks,

I always end up declaring them as self._blah_

will use this, deffo.

Florian AÃŸmann wrote:

> instead of mixin in a whole helper module I’d declare the Helper method  
> as module\_function like:
> 
> module SomeHelper  
> 1000.times do |index|  
> define\_method(:“waste\_#{index}”) { index \*\* 2 }  
> end  
> def some\_method(\*some\_args)  
> some\_args.inspect  
> end
> 
> module\_function :some\_method  
> end

---

<div class="post-metadata">

**Author:** ![joep](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/87869e/32.png) [@joep](https://www.ruby-forum.com/u/joep)\
**Post date:** [July 15, 2007, 6:58am UTC](https://www.ruby-forum.com/t/quick-question-method-in-controller-and-helper/107261/7 "2007-07-15T06:58:19Z")

</div>

ahoge wrote:

> Is it possible to have method(s) in application\_helper.rb available  
> for controllers, just like for views?  
> Actually my method is current\_user too 😃 and looks pretty similar,  
> but I’d like it in application\_helper.rb…

define the method in your helper, then include your helper at the top of  
the controller:

class ApplicationHelper  
def method\_to\_use\_in\_controller  
…  
end  
end

class ApplicationController  
include ApplicationHelper  
end

that’s the easiest way to do it … but if you have a lot of methods in  
your ApplicationHelper you may want to pull it out into a module that  
gets included into both the helper and controller.

---

<div class="post-metadata">

**Author:** ![joep](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/87869e/32.png) [@joep](https://www.ruby-forum.com/u/joep)\
**Post date:** [July 16, 2007, 3:41am UTC](https://www.ruby-forum.com/t/quick-question-method-in-controller-and-helper/107261/8 "2007-07-16T03:41:42Z")

</div>

Thanks
