5

I have a Rails 3 based CMS that allows users to create and modify layouts and views. These layouts and views are the same ones built into the framework, only backed by a model for some additional capabilities. The problem I would like to address is that these template files are cached as soon as they are accessed on the public end, so it is not possible to see changes in the layouts or views unless the server is restarted. This does not occur in development mode where caching is disabled, but obviously turning off template caching in production wouldn't be great for performance. Clearing memcache doesn't seem to do the trick. Is it possible to programatically clear out the layouts and views cache in production, perhaps with something like reload! like we have in the console? Or am I stuck having to restart Passenger every time someone wants to tweak one of these layouts or views (perhaps using the approach in this thread: Rails Cache Clearing)?

Please note that I am not referring to clearing the page and action caches, which the public pages rely on and works just fine.

braX
  • 11,506
  • 5
  • 20
  • 33
cmarin
  • 51
  • 2

3 Answers3

2

this configuration may help (at least it worked* for me):

config.action_view.cache_template_loading = false
  • works in rails 3

There's just a slight difference in rails 2:

config.action_view.cache_template_reloading = false
Evgeniya Manolova
  • 2,542
  • 27
  • 21
2

José Valim has a great chapter in "Crafting Rails Applications" that goes over this topic. Here is an approach that uses Mongoid to store view templates. If you build your own view Resolver, then you just need to call #clear_cache on the resolver instance when someone saves a new template in the database.

monocle
  • 5,866
  • 2
  • 26
  • 22
  • @monocle-thank you for that link to the relevant chapter within Crafting Rails Applications, I'll take a look at that. – cmarin May 04 '11 at 00:03
1

In production mode, it's normal to require a restart to implement rails code changes, which is what you are doing by editing the layouts and views. It sounds like you're really operating in a development environment if you are editing the application code while it's running. In production mode I don't know of a way to refresh Passenger without touching restart.txt or restarting the web server.

EDIT: You should be able to touch tmp/restart.txt programmatically from within your app. This should tell Passenger to reload on the next request.

heyrolled
  • 451
  • 4
  • 13
  • Definitely understand that this isn't a typical Rails app where layout and view changes (along with any other code) live in a source control system and require a restart for those modifications to take effect. In fact the admin side operates just like this. This app just has that special use case where ideally, the layouts and views that control the public pages could ideally be changed without requiring a server restart. – cmarin May 02 '11 at 23:48
  • @cmarin - This sounds like an interesting special case. Can you explain further details as to why you need this special requirement? Maybe it will help us find a good answer for you (i.e. maybe something exists out there that we can point you to). – jefflunt May 02 '11 at 23:54
  • Maybe the parts of the views that change often should be part of the CMS system the allows them to be edited and stored in the database. – heyrolled May 03 '11 at 00:08
  • This app was first built on Rails 1.x. At that time at least, it seemed like the simplest and DRYest approach for allowing users to create public pages was to reuse the existing layout and view functionality built into Rails, which required that those files exist within the file system. Basically, we have Layout and View ActiveRecord models, which store the the ERB and HTML for the templates within the db for the public facing pages. Whenever these are created or edited, the system spits out files into a custom directory (rails_root/ui/layouts and /rails_root/ui/views). – cmarin May 04 '11 at 00:12
  • These Layouts and Views for the public facing side are separate from the views and layouts in /rails_root/app/views, which by and large control the admin side of the system. The content that is created is also obviously stored in the db, and it makes use of these views and layouts. There's no problem clearing the cache for those pages. The issue is simply that the layouts and views that the public pages rely on are cached and we can't seem to clear them without restarting the server. – cmarin May 04 '11 at 00:13
  • Well if you are running Passenger, I guess a hack may be to touch your tmp/restart.txt file from within your app. – heyrolled May 04 '11 at 16:01