Ruby Forum Ruby on Rails > Getting fragment-caching to work...

Posted by Peter (Guest)
on 09.03.2008 14:17
(Received via mailing list)
I'd having my first tries at caching in Rails, and would like to
experiment with fragmentcaching. I have some problems with getting any
results and i don't know exactly what's going on now.

I have this in my development.rb...

config.action_controller.perform_caching             = true
config.action_controller.cache_store                   = :file_store,
RAILS_ROOT + "/public/cache/"

and this is my layout-viewfile...

- cache "main_menu" do
  = render :partial => 'menu'

I see nothing happening though, and no files appear in public/cache,
so i'm wondering if i overlooked something. The menu-partial loops
through all categories (it has a Category.find(:all).each in it) but i
still see that SQL-query in my terminal.
Posted by Nathan Esquenazi (xgamerx)
on 10.03.2008 01:00
You messed up the cache_store setting a bit.

# /config/environments/development.rb
config.action_controller.perform_caching = true
config.action_controller.fragment_cache_store = [:file_store, 
"#{RAILS_ROOT}/tmp/cache"]

Then make sure to restart the server.

That really should work as long as the "find" action is contained 
directly within the partial and is not done in the controller.
Posted by Peter (Guest)
on 11.03.2008 23:43
(Received via mailing list)
hmmm, i'm getting a

undefined method `cache_fragment' for #<Haml::Template:0x2647a74>

when i do that. is haml in the way?

On Mar 10, 1:00 am, Nathan Esquenazi <rails-mailing-l...@andreas-
Posted by Nathan Esquenazi (xgamerx)
on 12.03.2008 00:41
No, I am developing with HAML and I use fragment caching all the time. 
Are you using Rails 2.0.2. It should work like this:

# config/environments/development.rb
config.action_controller.perform_caching = true
config.action_controller.fragment_cache_store = [:file_store, 
"#{RAILS_ROOT}/tmp/cache"]

# app/views/example/something.html.haml

#...
- cache(:controller => "example", :action => "something") do
  <div id="some_section">
    this section will be cached
  </div>
#...

That is how I have it all throughout my app and it seems to work.
Posted by Nathan Esquenazi (xgamerx)
on 12.03.2008 00:43
Make sure you have the latest version of haml as well. Haml 1.8.2