# Action\_cacheing

**URL:** <https://www.ruby-forum.com/t/action-cacheing/75522>\
**Category:** Rails\
**Created:** [November 10, 2006, 5:59pm UTC](https://www.ruby-forum.com/t/action-cacheing/75522 "2006-11-10T17:59:43Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Henrik\_Jensen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/65b543/32.png) [@Henrik\_Jensen](https://www.ruby-forum.com/u/Henrik_Jensen)\
**Post date:** [November 10, 2006, 5:59pm UTC](https://www.ruby-forum.com/t/action-cacheing/75522/1 "2006-11-10T17:59:43Z")

</div>

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 😕  
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?

---

<div class="post-metadata">

**Author:** ![Henrik\_Jensen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/65b543/32.png) [@Henrik\_Jensen](https://www.ruby-forum.com/u/Henrik_Jensen)\
**Post date:** [November 10, 2006, 6:28pm UTC](https://www.ruby-forum.com/t/action-cacheing/75522/2 "2006-11-10T18:28:58Z")

</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 😕

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|  
exec\_this = "  
if (getCookie(‘logged\_in’)  
{  
$(‘login\_link’).href = ‘/logout’;  
$(‘login\_link’).innerHTML = ‘logout’;"  
}"

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 tags…

//\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
function getCookie(name) {  
var dc = document.cookie;  
var prefix = name + “=”;  
var begin = dc.indexOf("; " + prefix);  
if (begin == -1) {  
begin = dc.indexOf(prefix);  
if (begin != 0) return null;  
} else  
begin += 2;  
var end = document.cookie.indexOf(";", begin);  
if (end == -1)  
end = dc.length;  
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 's, give this  
> a shot and see if it works for you…

Hope you find this useful!

Cheery-o!  
Gustav P.  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![Henrik\_Jensen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/65b543/32.png) [@Henrik\_Jensen](https://www.ruby-forum.com/u/Henrik_Jensen)\
**Post date:** [November 10, 2006, 7:30pm UTC](https://www.ruby-forum.com/t/action-cacheing/75522/3 "2006-11-10T19:30:04Z")

</div>

> 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.

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?

---

<div class="post-metadata">

**Author:** ![Henrik\_Jensen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/65b543/32.png) [@Henrik\_Jensen](https://www.ruby-forum.com/u/Henrik_Jensen)\
**Post date:** [November 11, 2006, 5:42pm UTC](https://www.ruby-forum.com/t/action-cacheing/75522/4 "2006-11-11T17:42:00Z")

</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.

–  
Building an e-commerce site with Rails?  
[http://www.agilewebdevelopment.com/rails-ecommerce](http://www.agilewebdevelopment.com/rails-ecommerce)

---

<div class="post-metadata">

**Author:** ![Henrik\_Jensen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/65b543/32.png) [@Henrik\_Jensen](https://www.ruby-forum.com/u/Henrik_Jensen)\
**Post date:** [November 10, 2006, 7:47pm UTC](https://www.ruby-forum.com/t/action-cacheing/75522/5 "2006-11-10T19:47:10Z")

</div>

Henrik Jensen wrote:

> 

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 P.  
removed\_email\_address@domain.invalid
