# Prevent caching of helper method

**URL:** <https://www.ruby-forum.com/t/prevent-caching-of-helper-method/169193>\
**Category:** Rails\
**Created:** [May 31, 2009, 12:12pm UTC](https://www.ruby-forum.com/t/prevent-caching-of-helper-method/169193 "2009-05-31T12:12:58Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![chris](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/958977/32.png) [@chris](https://www.ruby-forum.com/u/chris)\
**Post date:** [May 31, 2009, 12:12pm UTC](https://www.ruby-forum.com/t/prevent-caching-of-helper-method/169193/1 "2009-05-31T12:12:58Z")

</div>

Hi,  
I have a helper method to get a date from a cookie (in application  
helper) that looks like this:

def getToday  
if(cookies[:date])  
cookies[:date].to\_date  
else  
Date.today  
end  
end

I set the cooke date using a before\_filter method in the application  
controller, reading a GET param:

before\_filter :save\_today\_as\_cookie

def save\_today\_as\_cookie

```
if params[:d]
  cookies.delete :date
  cookies[:date] = {
    :value => params[:d],
    :expires => 1.hours.from_now
  }
end

```

end

This works fine, but if I want to change the date by passing a new  
date parameter, I have to reload the page twice that it takes affect.

The cookie changes instantly. It seems to me, that the result of the  
helper function is cached for one more load.  
Setting expiring headers or etags did not help.

I’m using Rails 2.3.2 and no special cache plugin/gem/configuration.

Regards,

Chris

---

<div class="post-metadata">

**Author:** ![chris](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/958977/32.png) [@chris](https://www.ruby-forum.com/u/chris)\
**Post date:** [May 31, 2009, 1:35pm UTC](https://www.ruby-forum.com/t/prevent-caching-of-helper-method/169193/2 "2009-05-31T13:35:29Z")

</div>

On May 31, 11:12 am, Chris [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> The cookie changes instantly. It seems to me, that the result of the  
> helper function is cached for one more load.  
> Setting expiring headers or etags did not help.

It’s not your helper function, it’s the cookies method:  
[https://rails.lighthouseapp.com/projects/8994/tickets/2733](https://rails.lighthouseapp.com/projects/8994/tickets/2733)

Fred

---

<div class="post-metadata">

**Author:** ![chris](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/958977/32.png) [@chris](https://www.ruby-forum.com/u/chris)\
**Post date:** [May 31, 2009, 4:35pm UTC](https://www.ruby-forum.com/t/prevent-caching-of-helper-method/169193/3 "2009-05-31T16:35:01Z")

</div>

Ah, thanks for the hint. So I’ll have to update the actionpack/lib/  
action\_controller/base/cookies.rb file, right?

Or what is the recommended way to fix problems in the rails core? (I’m  
pretty new to rails)

Regards,

Chris

---

<div class="post-metadata">

**Author:** ![chris](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/958977/32.png) [@chris](https://www.ruby-forum.com/u/chris)\
**Post date:** [May 31, 2009, 4:59pm UTC](https://www.ruby-forum.com/t/prevent-caching-of-helper-method/169193/4 "2009-05-31T16:59:01Z")

</div>

On May 31, 3:37 pm, Chris [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I mean is there a way to patch/overwrite this using a plugin or  
> something? I don’t want to touch the core classes, if not necessary.

You could just monkey patch it using the patch on that ticket as a  
guide.

Fred

---

<div class="post-metadata">

**Author:** ![chris](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/958977/32.png) [@chris](https://www.ruby-forum.com/u/chris)\
**Post date:** [May 31, 2009, 4:38pm UTC](https://www.ruby-forum.com/t/prevent-caching-of-helper-method/169193/5 "2009-05-31T16:38:36Z")

</div>

I mean is there a way to patch/overwrite this using a plugin or  
something? I don’t want to touch the core classes, if not necessary.

---

<div class="post-metadata">

**Author:** ![chris](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/958977/32.png) [@chris](https://www.ruby-forum.com/u/chris)\
**Post date:** [June 1, 2009, 10:03am UTC](https://www.ruby-forum.com/t/prevent-caching-of-helper-method/169193/6 "2009-06-01T10:03:29Z")

</div>

Ah, I see. Thanks!

On May 31, 4:57 pm, Frederick C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
