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