# Getting fragment-caching to work

**URL:** <https://www.ruby-forum.com/t/getting-fragment-caching-to-work/131405>\
**Category:** Rails\
**Created:** [March 9, 2008, 2:17pm UTC](https://www.ruby-forum.com/t/getting-fragment-caching-to-work/131405 "2008-03-09T14:17:45Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![peter](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/278dde/32.png) [@peter](https://www.ruby-forum.com/u/peter)\
**Post date:** [March 9, 2008, 2:17pm UTC](https://www.ruby-forum.com/t/getting-fragment-caching-to-work/131405/1 "2008-03-09T14:17:45Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![peter](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/278dde/32.png) [@peter](https://www.ruby-forum.com/u/peter)\
**Post date:** [March 11, 2008, 11:43pm UTC](https://www.ruby-forum.com/t/getting-fragment-caching-to-work/131405/2 "2008-03-11T23:43:57Z")

</div>

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 E. \<rails-mailing-l…@andreas-

---

<div class="post-metadata">

**Author:** ![peter](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/278dde/32.png) [@peter](https://www.ruby-forum.com/u/peter)\
**Post date:** [March 12, 2008, 12:41am UTC](https://www.ruby-forum.com/t/getting-fragment-caching-to-work/131405/3 "2008-03-12T00:41:47Z")

</div>

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
 this section will be cached

#…

That is how I have it all throughout my app and it seems to work.

---

<div class="post-metadata">

**Author:** ![peter](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/278dde/32.png) [@peter](https://www.ruby-forum.com/u/peter)\
**Post date:** [March 10, 2008, 1:00am UTC](https://www.ruby-forum.com/t/getting-fragment-caching-to-work/131405/4 "2008-03-10T01:00:14Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![peter](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/278dde/32.png) [@peter](https://www.ruby-forum.com/u/peter)\
**Post date:** [March 12, 2008, 12:43am UTC](https://www.ruby-forum.com/t/getting-fragment-caching-to-work/131405/5 "2008-03-12T00:43:04Z")

</div>

Make sure you have the latest version of haml as well. Haml 1.8.2
