# "Helper" for 2 controllers?

**URL:** <https://www.ruby-forum.com/t/helper-for-2-controllers/166325>\
**Category:** Rails\
**Created:** [April 23, 2009, 9:47pm UTC](https://www.ruby-forum.com/t/helper-for-2-controllers/166325 "2009-04-23T21:47:27Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![cojones](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/9f8e36/32.png) [@cojones](https://www.ruby-forum.com/u/cojones)\
**Post date:** [April 23, 2009, 9:47pm UTC](https://www.ruby-forum.com/t/helper-for-2-controllers/166325/1 "2009-04-23T21:47:27Z")

</div>

Is there a DRY way to have a helper method for 2 different controllers?

For example:  
@user.create\_points\_account(10)  
@group.create\_points\_account(10)

and the helper method would look like this:

def create\_points\_account pts  
f = PointAccount.new(:points =\> pts)  
f.resource = self  
return false if !f.save  
end

---

<div class="post-metadata">

**Author:** ![cojones](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/9f8e36/32.png) [@cojones](https://www.ruby-forum.com/u/cojones)\
**Post date:** [April 23, 2009, 9:50pm UTC](https://www.ruby-forum.com/t/helper-for-2-controllers/166325/2 "2009-04-23T21:50:12Z")

</div>

Place it in a module somewhere and have both controllers to include that  
module.

- 

Maurício Linhares  
[http://alinhavado.wordpress.com/](http://alinhavado.wordpress.com/) (pt-br) | [http://blog.codevader.com/](http://blog.codevader.com/)  
(en)

On Thu, Apr 23, 2009 at 4:47 PM, Heinz S.

---

<div class="post-metadata">

**Author:** ![cojones](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/9f8e36/32.png) [@cojones](https://www.ruby-forum.com/u/cojones)\
**Post date:** [April 23, 2009, 10:03pm UTC](https://www.ruby-forum.com/t/helper-for-2-controllers/166325/3 "2009-04-23T22:03:58Z")

</div>

I included it in the controller with helper :points and called it like  
above but I get an error:  
undefined method create\_points\_account

What could be the problem?

MaurÃ­cio Linhares wrote:

> Place it in a module somewhere and have both controllers to include that  
> module.
> 
> - 
> 
> Maurï¿½cio Linhares  
> [http://alinhavado.wordpress.com/](http://alinhavado.wordpress.com/) (pt-br) | [http://blog.codevader.com/](http://blog.codevader.com/)  
> (en)
> 
> On Thu, Apr 23, 2009 at 4:47 PM, Heinz S.

---

<div class="post-metadata">

**Author:** ![cojones](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/9f8e36/32.png) [@cojones](https://www.ruby-forum.com/u/cojones)\
**Post date:** [April 23, 2009, 10:41pm UTC](https://www.ruby-forum.com/t/helper-for-2-controllers/166325/4 "2009-04-23T22:41:13Z")

</div>

I changed it from helper :fund to include Fund and it’s included now but  
it doesn’t really help cause there’s still the error:  
undefined method `create\_fund’ for #User:0x5d52c6c

---

<div class="post-metadata">

**Author:** ![cojones](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/9f8e36/32.png) [@cojones](https://www.ruby-forum.com/u/cojones)\
**Post date:** [April 23, 2009, 10:08pm UTC](https://www.ruby-forum.com/t/helper-for-2-controllers/166325/5 "2009-04-23T22:08:25Z")

</div>

I guess I did not made myself clear.

I said create a module somewhere (usually under the lib folder), like:

#lib/points.rb

module Points

def create\_points\_account( pts )  
f = PointAccount.new(:points =\> pts)  
f.resource = self  
return false if !f.save  
end

end

And then at your controller:

class SomeController \< ActionController::Base

include Points

end

- 

MaurÃ­cio Linhares  
[http://alinhavado.wordpress.com/](http://alinhavado.wordpress.com/) (pt-br) | [http://blog.codevader.com/](http://blog.codevader.com/)  
(en)

On Thu, Apr 23, 2009 at 5:03 PM, Heinz S.
