# Using helper\_method from a Mixin Module

**URL:** <https://www.ruby-forum.com/t/using-helper-method-from-a-mixin-module/111817>\
**Category:** Rails\
**Created:** [August 11, 2007, 3:05pm UTC](https://www.ruby-forum.com/t/using-helper-method-from-a-mixin-module/111817 "2007-08-11T15:05:54Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![johnlester](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e480ec/32.png) [@johnlester](https://www.ruby-forum.com/u/johnlester)\
**Post date:** [August 11, 2007, 3:05pm UTC](https://www.ruby-forum.com/t/using-helper-method-from-a-mixin-module/111817/1 "2007-08-11T15:05:54Z")

</div>

Hi,

I have a module with a helper method in it. I want to automatically  
declare the method as a helper when I mix the module into a controller  
class, so the mixin basically sets itself up. Something like :-

module MyMixin

# I want to do helper method here in the module

helper\_method :my\_helper

def my\_helper  
puts “foo”  
end  
end

* * *

class MyController \< ApplicationController

include MyMixin

# I dont’ want to have to do helper\_method in the controller

# helper\_method :my\_helper

* * *

This does not work as I want, I have to do “helper\_method” in the  
controller class itself. If I try to do it in the module I get an error  
like this :-

undefined method `helper method` for MyMixin:Module

Is there a way to achieve this?

---

<div class="post-metadata">

**Author:** ![johnlester](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e480ec/32.png) [@johnlester](https://www.ruby-forum.com/u/johnlester)\
**Post date:** [August 11, 2007, 3:43pm UTC](https://www.ruby-forum.com/t/using-helper-method-from-a-mixin-module/111817/2 "2007-08-11T15:43:45Z")

</div>

Hi –

On Sat, 11 Aug 2007, John L. wrote:

> helper\_method :my\_helper  
> include MyMixin  
> undefined method `helper method` for MyMixin:Module
> 
> Is there a way to achieve this?

Try this (untested):

module MyMixin  
def self.included(c)  
c.helper\_method :my\_helper  
end

```
 def my_helper
   etc.
 end

```

end

The idea is to call the included callback, which allows you to operate  
on the class that’s doing the including.

David

–

- Books:  
RAILS ROUTING (new! [http://www.awprofessional.com/title/0321509242](http://www.awprofessional.com/title/0321509242))  
RUBY FOR RAILS ([Ruby for Rails](http://www.manning.com/black))
- Ruby/Rails training  
& consulting: Ruby Power and Light, LLC ([http://www.rubypal.com](http://www.rubypal.com))

---

<div class="post-metadata">

**Author:** ![johnlester](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e480ec/32.png) [@johnlester](https://www.ruby-forum.com/u/johnlester)\
**Post date:** [August 11, 2007, 5:42pm UTC](https://www.ruby-forum.com/t/using-helper-method-from-a-mixin-module/111817/3 "2007-08-11T17:42:37Z")

</div>

Many thanks for this, it works beautifully!

> module MyMixin  
> def self.included©  
> c.helper\_method :my\_helper  
> end
