# \[tip\] making cached pages look dynamic

**URL:** <https://www.ruby-forum.com/t/tip-making-cached-pages-look-dynamic/56113>\
**Category:** Rails\
**Created:** [March 28, 2006, 10:56am UTC](https://www.ruby-forum.com/t/tip-making-cached-pages-look-dynamic/56113 "2006-03-28T10:56:34Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![mikkel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e36b37/32.png) [@mikkel](https://www.ruby-forum.com/u/mikkel)\
**Post date:** [March 28, 2006, 10:56am UTC](https://www.ruby-forum.com/t/tip-making-cached-pages-look-dynamic/56113/1 "2006-03-28T10:56:34Z")

</div>

Hey

I just launched a new release of www.strongside.dk based on rails 1.1.

The site is in danish so it wont make any sense to most of you.

However, here at technique I want to share with you.

Some of the pages on that site requires a lot of calculations, but the  
data is actually static. So these pages are cached.

However, when a logged on user is viewing a cached page, we still want  
to show him links based on his profile.

Enter, a litte rjs…

1. in my layout i have:

 [Login](https://www.ruby-forum.com/account/login) |

```
<a href="/account/signup">Opret Profil</a>
 |

```

 

but when a user is logged in I want to replace these with a logout and a  
view profile link.

1. create a action and add a whatever.js route for it. I called my route  
“profile.js”.  
Just below my navigation is call this action with

1. implement the action

def profile  
render :update do |page|  
if session[‘user’]  
page.replace\_html ‘login\_links’,:inline=\>‘Log af | [Min  
Profil](https://www.ruby-forum.com/viewprofile) |’  
end  
end  
end

I know, I should use the url helpers for this…

Anyways…

When a page is shown on the site, it only contains the login and create  
profile links.  
It calls the rjs action through the profile.js import  
The rjs action checks if its an logged in user, if so, replace the  
login/  
create profile links with logout/view profile.

This technique is in part inspired by one shown previously at http://  
caboo.se, but this one, using rjs is much simpler…

Mikkel B.

www.strongside.dk - Football Portal(DK)  
[nflfeed.helenius.org](http://nflfeed.helenius.org) - Football News(DK)  
ting.minline.dk - Buy Old Stuff!(DK)

---

<div class="post-metadata">

**Author:** ![mikkel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e36b37/32.png) [@mikkel](https://www.ruby-forum.com/u/mikkel)\
**Post date:** [March 28, 2006, 9:24pm UTC](https://www.ruby-forum.com/t/tip-making-cached-pages-look-dynamic/56113/2 "2006-03-28T21:24:01Z")

</div>

Mikkel B. wrote:

> However, when a logged on user is viewing a cached page, we still want  
> to show him links based on his profile.
> 
> Enter, a litte rjs…

…

> When a page is shown on the site, it only contains the login and create  
> profile links.  
> It calls the rjs action through the profile.js import  
> The rjs action checks if its an logged in user, if so, replace the login/  
> create profile links with logout/view profile.

Interesting.

I had been thinking about this problem myself. I have a couple of pretty  
expensive pages that I was thinking about removing the customizations  
from so that they would be served from the cache.

The solution that I was contemplating was to move the customizations  
into partials. Then, I was thinking, that the main page would be cached,  
but the partials would be dynamically generated.

Does anyone know 1) if my mental model of caching is even correct and 2)  
how the two approaches would compare?

Thanks,

Ray

---

<div class="post-metadata">

**Author:** ![mikkel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e36b37/32.png) [@mikkel](https://www.ruby-forum.com/u/mikkel)\
**Post date:** [March 28, 2006, 9:48pm UTC](https://www.ruby-forum.com/t/tip-making-cached-pages-look-dynamic/56113/3 "2006-03-28T21:48:56Z")

</div>

page caching writes the view to an actual .html file in public, so its  
completely static and partials wouldnt help you.  
You might have more luck with fragment cache, but is you have a lot of  
data in the fragment, that might cause a problem???

On Tuesday, March 28, 2006, at 11:09 AM, Ray B. wrote:

> > It calls the rjs action through the profile.js import  
> > The solution that I was contemplating was to move the customizations
> 
> * * *
> 
> Rails mailing list  
> removed\_email\_address@domain.invalid  
> [http://lists.rubyonrails.org/mailman/listinfo/rails](http://lists.rubyonrails.org/mailman/listinfo/rails)

Mikkel B.

www.strongside.dk - Football Portal(DK)  
[nflfeed.helenius.org](http://nflfeed.helenius.org) - Football News(DK)  
ting.minline.dk - Buy Old Stuff!(DK)

---

<div class="post-metadata">

**Author:** ![mikkel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e36b37/32.png) [@mikkel](https://www.ruby-forum.com/u/mikkel)\
**Post date:** [March 28, 2006, 10:16pm UTC](https://www.ruby-forum.com/t/tip-making-cached-pages-look-dynamic/56113/4 "2006-03-28T22:16:35Z")

</div>

If I were you I would take a look at partial page caching. It looks  
something like:  
\<% cache(“file/path/here”) do %\>  
\<%= render :whatever %\>  
\<% end %\>

This will cache whatever is in the block to the file given. If the  
file isn’t there, then it will create it and the output as needed. I  
use this method quite a bit since I have a header which changes when a  
user is logged in or not. It seems to work pretty well.

-Nick
