# action\_cacheing

**URL:** <https://discuss.rubyonrails.org/t/action-cacheing/4364>\
**Category:** rubyonrails-talk\
**Created:** [November 10, 2006, 5:25pm UTC](https://discuss.rubyonrails.org/t/action-cacheing/4364 "2006-11-10T17:25:23Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Gustav\_Railist](https://avatars.discourse-cdn.com/v4/letter/g/d2c977/32.png) [@Gustav\_Railist](https://discuss.rubyonrails.org/u/Gustav_Railist)\
**Post date:** [November 10, 2006, 5:25pm UTC](https://discuss.rubyonrails.org/t/action-cacheing/4364/1 "2006-11-10T17:25:23Z")

</div>

Hey

Henrik Jensen wrote:

> I'm playing with caching, and since my site contians a "login and register" link, which only should be shown when the user isn't logged in, page-caching is not an option 😕&nbsp;&nbsp;

I thought the same thing a while ago, but then I played around with it a bit and eventually javascript semi-solved the problem.

By saving an additional cookie in users' browser - one which only existed while they were logged in - I used javascript to check for it (findiing it meaning they were logged in) and changing tiny bits of my layout, eg the login link to a logout link if the cookie existed.

Here's the code:

In the view:

\<%= link\_to 'login', '/login', :id =\> 'login\_link' %\>

\<%= update\_page\_tag do |page| &nbsp;&nbsp;exec\_this = " &nbsp;&nbsp;if (getCookie('logged\_in') &nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;$('login\_link').href = '/logout'; &nbsp;&nbsp;&nbsp;&nbsp;$('login\_link').innerHTML = 'logout';" &nbsp;&nbsp;}"

&nbsp;&nbsp;page \<\< exec\_this

end %\>

The javascript looks as follows and I got it from a website sometime, I think...so kudos to whoever wrote it! I think it returns the actual value of the cookie, so you can use that if you'd like.

Just put this in your application.js, or in \<script\> tags...

//\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\* function getCookie(name) { &nbsp;&nbsp;var dc = document.cookie; &nbsp;&nbsp;var prefix = name + "="; &nbsp;&nbsp;var begin = dc.indexOf("; " + prefix); &nbsp;&nbsp;if (begin == -1) { &nbsp;&nbsp;&nbsp;&nbsp;begin = dc.indexOf(prefix); &nbsp;&nbsp;&nbsp;&nbsp;if (begin != 0) return null; &nbsp;&nbsp;} else &nbsp;&nbsp;&nbsp;&nbsp;begin += 2; &nbsp;&nbsp;var end = document.cookie.indexOf(";", begin); &nbsp;&nbsp;if (end == -1) &nbsp;&nbsp;&nbsp;&nbsp;end = dc.length; &nbsp;&nbsp;return unescape(dc.substring(begin + prefix.length, end)); } //\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*

> but what about action-caching? the to links are placed in layout/application.rhtml, but they are still being cached 😕 shouldn't caches\_action only cache the output of the action i use i on

I don't think this'll work for very large modifications, but if the only thing holding you back from page caching is one or two \<a\>'s, give this a shot and see if it works for you...

Hope you find this useful!

Cheery-o! Gustav Paul gustav@rails.co.za

---

<div class="post-metadata">

**Author:** ![Gustav\_Railist](https://avatars.discourse-cdn.com/v4/letter/g/d2c977/32.png) [@Gustav\_Railist](https://discuss.rubyonrails.org/u/Gustav_Railist)\
**Post date:** [November 10, 2006, 6:42pm UTC](https://discuss.rubyonrails.org/t/action-cacheing/4364/2 "2006-11-10T18:42:33Z")

</div>

Henrik Jensen wrote:

> > By saving an additional cookie in users' browser - one which only existed while they were logged in - I used javascript to check for it (findiing it meaning they were logged in) and changing tiny bits of my layout, eg the login link to a logout link if the cookie existed. &nbsp;&nbsp;&nbsp;&nbsp; I guess that would work - however I have to admit it's ugly, and I would prefer not to use it 🙂
> 
> Is there really no other way to only catch the returnvalue of an action?

Lol, I preferred not to use it either in the end 🙂

Nope, I don't know of any other way. This bugged me as well, but I couldn't find any other way ☹

Sorry man, Gustav Paul gustav@rails.co.za

---

<div class="post-metadata">

**Author:** ![Benjamin\_Curtis1](https://avatars.discourse-cdn.com/v4/letter/b/9de0a6/32.png) [@Benjamin\_Curtis1](https://discuss.rubyonrails.org/u/Benjamin_Curtis1)\
**Post date:** [November 11, 2006, 4:37pm UTC](https://discuss.rubyonrails.org/t/action-cacheing/4364/3 "2006-11-11T16:37:56Z")

</div>

You might be better served with the action\_cache plugin, [http://agilewebdevelopment.com/plugins/action\_cache](http://agilewebdevelopment.com/plugins/action_cache), which can use any variables available to the app (such as logged-in status) to create alternate cached versions of the same action.
