Rails template caching intricasies

You just have to love coding. I mean, unless you don’t, you’re not likely to put up with two hours of nearly useless debugging, when you realize your WeBRICK or Mongrel development environment do not work as it should. That’s exactly what I just did, and in order to help others, I’ll give you the details so you can get some decent results when Googling.

The problem: I wanted to do some quick template prototyping. Hitting my WebFaction rails development environment, I started to make changes to a page template, update it, make changes, etc. Only this time no changes were shown after initial load! Only a server restart would make the changes visible. WTF. This isn’t how the development environment should work at all!

The googling: After some carefully selected phrases in Google, a few things soon became apparent:

  1. No-one else seems to have encountered the problem and have a solution
  2. Rails documentation on production/development configuration is nonexistent
  3. Multiple how-tos existed how to enable caching, but not for disabling it

I tried to add the following configuration directives to config/environment/development.rb:

  • config.cache_classes = false (actually, this was there already)
  • config.action_view.cache_template_extensions = false (ditto)
  • config.action_view.cache_template_loading = false (this directive was in the production, but not in development, dunno why)

None of this stuff worked. Switching to WeBRICK from Mongrel didn’t work. However, cache_classes directive seemed to make changes to controllers visible, but view changes were still invisible until a server restart.

The revelation: Finally, after little more than one hour of trial-and-error, I finally found the vast amount of documentation available on template caching on Rails. For those who haven’t yet found it from Rails API, it is located under ActionView::Base. I will reproduce it here to highlight the detail in which template caching and its configuration directives are explained:

By default, Rails will compile each template to a method in order to render it. When you alter a template, Rails will check the file‘s modification time and recompile it.

Now I had sufficient information to reach the solution. For some odd reason, my combination of Notepad++, WinSCP and WebFaction server setup caused the timestamp of saved template file to be one hour off. This caused Rails to conclude, that no changes had been made since last compiling the template (oh, they would be noticed after waiting for one hour, but who wants to wait one hour after every change?).

The solution: This was a stupid problem, but the solution evaded me for too long because of scarce (and hard to find) documentation on template caching mechanism. A simple touch command after changes now does the trick. A hack, but it works well enough.

Published by

Joonas Pihlajamaa

Coding since 1990 in Basic, C/C++, Perl, Java, PHP, Ruby and Python, to name a few. Also interested in math, movies, anime, and the occasional slashdot now and then. Oh, and I also have a real life, but lets not talk about it!

One thought on “Rails template caching intricasies”

Leave a Reply

Your email address will not be published. Required fields are marked *

two × = 4

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>