# How to cache this kind of action?

**URL:** <https://www.ruby-forum.com/t/how-to-cache-this-kind-of-action/85284>\
**Category:** Rails\
**Created:** [February 12, 2007, 11:02am UTC](https://www.ruby-forum.com/t/how-to-cache-this-kind-of-action/85284 "2007-02-12T11:02:39Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Tiger\_Dong](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/50afbb/32.png) [@Tiger\_Dong](https://www.ruby-forum.com/u/Tiger_Dong)\
**Post date:** [February 12, 2007, 11:02am UTC](https://www.ruby-forum.com/t/how-to-cache-this-kind-of-action/85284/1 "2007-02-12T11:02:39Z")

</div>

I have an action. It renders files specified by URL. If the file is a  
source code, the action will do something on it, like highlighting.  
If the file is a plaintext or an image, the action just sends it as  
is.

This code works until I turn on cache on this action. With cache  
enabled, the second time to access a URL will result in a prompt  
dialog asking if you want to download it.

I guess it is related to the mimetype. But don’t know how to solve it:(

---

<div class="post-metadata">

**Author:** ![Tiger\_Dong](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/50afbb/32.png) [@Tiger\_Dong](https://www.ruby-forum.com/u/Tiger_Dong)\
**Post date:** [February 13, 2007, 8:42am UTC](https://www.ruby-forum.com/t/how-to-cache-this-kind-of-action/85284/2 "2007-02-13T08:42:23Z")

</div>

The Rails action\_cache does not keep the headers returned from the  
first request that causes the data to be cached. On subsequent  
requests, if you set the correct response.headers for the file _after_  
the action cache before filter, then you will get the default headers  
(text/html content type)

My action\_cache filter will keep the headers returned, and will return  
the same ones on subsequent requests.

Just install the plugin and restart your app – for this fix, no other  
configuration is needed.

---

<div class="post-metadata">

**Author:** ![Tiger\_Dong](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/50afbb/32.png) [@Tiger\_Dong](https://www.ruby-forum.com/u/Tiger_Dong)\
**Post date:** [February 15, 2007, 7:30am UTC](https://www.ruby-forum.com/t/how-to-cache-this-kind-of-action/85284/3 "2007-02-15T07:30:27Z")

</div>

So where can I get your plugin? Thanks in advance.

---

<div class="post-metadata">

**Author:** ![Tiger\_Dong](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/50afbb/32.png) [@Tiger\_Dong](https://www.ruby-forum.com/u/Tiger_Dong)\
**Post date:** [February 15, 2007, 7:35am UTC](https://www.ruby-forum.com/t/how-to-cache-this-kind-of-action/85284/4 "2007-02-15T07:35:27Z")

</div>

I found it.  
[http://www.agilewebdevelopment.com/plugins/action\_cache](http://www.agilewebdevelopment.com/plugins/action_cache)

---

<div class="post-metadata">

**Author:** ![Tiger\_Dong](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/50afbb/32.png) [@Tiger\_Dong](https://www.ruby-forum.com/u/Tiger_Dong)\
**Post date:** [February 15, 2007, 8:38am UTC](https://www.ruby-forum.com/t/how-to-cache-this-kind-of-action/85284/5 "2007-02-15T08:38:32Z")

</div>

Ben’s agile development site is always the best place to find plugins.

---

<div class="post-metadata">

**Author:** ![Tiger\_Dong](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/50afbb/32.png) [@Tiger\_Dong](https://www.ruby-forum.com/u/Tiger_Dong)\
**Post date:** [February 15, 2007, 8:38pm UTC](https://www.ruby-forum.com/t/how-to-cache-this-kind-of-action/85284/6 "2007-02-15T20:38:02Z")

</div>

Are you setting the correct mime type in your action that returns the  
file? Can you post the code for that action?

---

<div class="post-metadata">

**Author:** ![Tiger\_Dong](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/50afbb/32.png) [@Tiger\_Dong](https://www.ruby-forum.com/u/Tiger_Dong)\
**Post date:** [February 16, 2007, 2:49am UTC](https://www.ruby-forum.com/t/how-to-cache-this-kind-of-action/85284/7 "2007-02-16T02:49:40Z")

</div>

Now I am using caches\_action instead of caches\_page and it works. Is  
this a “Work as design”?  
BTW: I think your plugin is useful. Can you make it default in rails?  
I am not clear about rails development model. Just wondering.

Anyway, thanks a lot.

---

<div class="post-metadata">

**Author:** ![Tiger\_Dong](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/50afbb/32.png) [@Tiger\_Dong](https://www.ruby-forum.com/u/Tiger_Dong)\
**Post date:** [February 15, 2007, 3:40pm UTC](https://www.ruby-forum.com/t/how-to-cache-this-kind-of-action/85284/8 "2007-02-15T15:40:15Z")

</div>

Opps, I’ve installed your plugin but still have the problem. It’s my  
first time to use rails plugin. I am not sure if I’ve done things  
right.

I installed the plugin by “./script/plugin install [http://craz8.com/](http://craz8.com/)  
svn/trunk/plugins/action\_cache”. But how can I know this plugin is  
working?
