Forum: Ruby on Rails [tip] making cached pages look dynamic

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
5d15c6821f3c3054c04b85471824ba7c?d=identicon&s=25 Mikkel Bruun (Guest)
on 2006-03-28 10:56
(Received via mailing list)
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:

<span id="login_links">
	<a href="/account/login">Login</a>
	 |

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

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

2. 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
<script src="/site/profile.js" type="text/javascript"></script>

3. implement the action

  def profile
    render :update do |page|
      if session['user']
        page.replace_html 'login_links',:inline=>'<span><a href="/
account/logout">Log af</a></span> | <span><a href="/viewprofile">Min
Profil</a></span> |'
      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 Bruun

www.strongside.dk    - Football Portal(DK)
nflfeed.helenius.org - Football News(DK)
ting.minline.dk      - Buy Old Stuff!(DK)
E28c35323f624b8b9ed8712e25105454?d=identicon&s=25 Ray Baxter (Guest)
on 2006-03-28 21:24
(Received via mailing list)
Mikkel Bruun 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
5d15c6821f3c3054c04b85471824ba7c?d=identicon&s=25 Mikkel Bruun (Guest)
on 2006-03-28 21:48
(Received via mailing list)
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 Baxter 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
>Rails@lists.rubyonrails.org
>http://lists.rubyonrails.org/mailman/listinfo/rails


Mikkel Bruun

www.strongside.dk    - Football Portal(DK)
nflfeed.helenius.org - Football News(DK)
ting.minline.dk      - Buy Old Stuff!(DK)
Cb610750ee94ca103aef4b2dc7b1b768?d=identicon&s=25 Nick Stuart (Guest)
on 2006-03-28 22:16
(Received via mailing list)
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
This topic is locked and can not be replied to.