# Private method in controller

**URL:** <https://www.ruby-forum.com/t/private-method-in-controller/109569>\
**Category:** Rails\
**Created:** [July 16, 2007, 11:30pm UTC](https://www.ruby-forum.com/t/private-method-in-controller/109569 "2007-07-16T23:30:20Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![leonb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/22d042/32.png) [@leonb](https://www.ruby-forum.com/u/leonb)\
**Post date:** [July 16, 2007, 11:30pm UTC](https://www.ruby-forum.com/t/private-method-in-controller/109569/1 "2007-07-16T23:30:20Z")

</div>

Hi!

I have two controllers. Number one has this private method:

private

def clean\_balance\_cache  
#Expire the cache for the module right  
expire\_fragment(:controller =\> ‘application’, :action =\> ‘balans’)  
end

Is there a way I can call this method in controller #2? I tried adding  
this method to the application helpers. But that didn’t work.  
expire\_fragment can’t be used in a helper. It’s an unknown method  
there.

Thank in advance!

---

<div class="post-metadata">

**Author:** ![leonb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/22d042/32.png) [@leonb](https://www.ruby-forum.com/u/leonb)\
**Post date:** [July 17, 2007, 12:03am UTC](https://www.ruby-forum.com/t/private-method-in-controller/109569/2 "2007-07-17T00:03:23Z")

</div>

LeonB schrieb:

> Is there a way I can call this method in controller #2? I tried adding  
> this method to the application helpers. But that didn’t work.  
> expire\_fragment can’t be used in a helper. It’s an unknown method  
> there.
> 
> Thank in advance!
> 
> >

Hi Leon,

just write a small lib, for example

./lib/cache\_helper.rb:

# can be used everywhere…

module CacheHelper  
def self.expire\_balance\_cache(controller)  
controller.expire\_fragment(:controller =\> ‘application’,  
:action =\> :balance)  
end  
# or as instance method  
def expire\_balance\_cache  
expire\_fragment(:controller =\> ‘application’,  
:action =\> :balance)  
end  
end

./app/controllers/balances\_controller.rb  
include CacheHelper # unless you want to keep your controller clean

def update  
#…  
if self.kind\_of? CacheHelper  
# do this if you included CacheHelper  
expire\_balance\_cache  
else  
# do this to keep your controller clean  
CacheHelper.expire\_balance\_cache(self)  
end  
#…  
end

Regards  
Florian

---

<div class="post-metadata">

**Author:** ![leonb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/22d042/32.png) [@leonb](https://www.ruby-forum.com/u/leonb)\
**Post date:** [July 19, 2007, 4:06am UTC](https://www.ruby-forum.com/t/private-method-in-controller/109569/3 "2007-07-19T04:06:14Z")

</div>

Thanks Florian! Gonna try that.
