Action_cacheing

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 :confused:
but what about action-caching? the to links are placed in
layout/application.rhtml, but they are still being cached :confused: shouldn’t
caches_action only cache the output of the action i use i on?

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 :confused:

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 :confused: 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.
[email protected]

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 :slight_smile:

Is there really no other way to only catch the returnvalue of an action?

You might be better served with the action_cache plugin, 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

Henrik Jensen wrote:

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

Nope, I don’t know of any other way. This bugged me as well, but I
couldn’t find any other way :frowning:

Sorry man,
Gustav P.
[email protected]