# Caching with Rails

**URL:** <https://www.ruby-forum.com/t/caching-with-rails/187178>\
**Category:** Rails\
**Created:** [April 19, 2010, 11:19am UTC](https://www.ruby-forum.com/t/caching-with-rails/187178 "2010-04-19T11:19:43Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![gregm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/bc79bd/32.png) [@gregm](https://www.ruby-forum.com/u/gregm)\
**Post date:** [April 19, 2010, 11:19am UTC](https://www.ruby-forum.com/t/caching-with-rails/187178/1 "2010-04-19T11:19:43Z")

</div>

Hi,

I’ve been reading the Rails Guide about caching and I have a few  
questions about it.  
I’ve read that it is not possible to cache a page with url parameters,  
for instance ‘http://…/com/products/category\_id=5’. Am I correct?  
Isn’t there any way to be able to do so?

If caching is only able on ‘conditions’ when do you use caching?

Thanks  
Greg

---

<div class="post-metadata">

**Author:** ![gregm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/bc79bd/32.png) [@gregm](https://www.ruby-forum.com/u/gregm)\
**Post date:** [April 19, 2010, 11:39am UTC](https://www.ruby-forum.com/t/caching-with-rails/187178/2 "2010-04-19T11:39:51Z")

</div>

> I’ve been reading the Rails Guide about caching and I have a few  
> questions about it.  
> I’ve read that it is not possible to cache a page with url parameters,  
> for instance ‘http://…/com/products/category\_id=5’. Am I correct?  
> Isn’t there any way to be able to do so?

You can do it, but it depends on how you format your URLs because it  
makes a  
static HTML file for Apache to serve (and Apache won’t serve files with  
GET  
parameters in the name). For example, if your URL is:

[http://www.example.com/products?category=5](http://www.example.com/products?category=5)

This wouldn’t work because Apache automatically separates on ? and would  
look for a file called products.html (not your category 5 specific one).

On the other hand if your URL was:

[http://www.example.com/products/category/5](http://www.example.com/products/category/5)

Then it would work - the 5 would become 5.html in a folder called  
category  
in a folder called products.

Caching works with parameters, just not normal GET parameters like  
?foo=bar.

Cheers,

Andy

---

<div class="post-metadata">

**Author:** ![gregm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/bc79bd/32.png) [@gregm](https://www.ruby-forum.com/u/gregm)\
**Post date:** [April 19, 2010, 11:50am UTC](https://www.ruby-forum.com/t/caching-with-rails/187178/3 "2010-04-19T11:50:45Z")

</div>

Andy J. wrote:

> You can do it, but it depends on how you format your URLs because it  
> makes a  
> static HTML file for Apache to serve (and Apache won’t serve files with  
> GET  
> parameters in the name). For example, if your URL is:
> 
> [http://www.example.com/products?category=5](http://www.example.com/products?category=5)
> 
> This wouldn’t work because Apache automatically separates on ? and would  
> look for a file called products.html (not your category 5 specific one).
> 
> On the other hand if your URL was:
> 
> [http://www.example.com/products/category/5](http://www.example.com/products/category/5)
> 
> Then it would work - the 5 would become 5.html in a folder called  
> category  
> in a folder called products.
> 
> Caching works with parameters, just not normal GET parameters like  
> ?foo=bar.
> 
> Cheers,
> 
> Andy

Thank you Andy for you explanation.  
How do you do to make your urls look like this?

Greg

---

<div class="post-metadata">

**Author:** ![gregm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/bc79bd/32.png) [@gregm](https://www.ruby-forum.com/u/gregm)\
**Post date:** [April 19, 2010, 12:48pm UTC](https://www.ruby-forum.com/t/caching-with-rails/187178/4 "2010-04-19T12:48:56Z")

</div>

> > [http://www.example.com/products/category/5](http://www.example.com/products/category/5)
> > 
> > Then it would work - the 5 would become 5.html in a folder called  
> > category  
> > in a folder called products.
> 
> Thank you Andy for you explanation.  
> How do you do to make your urls look like this?

config/routes.rb

map.category “/products/category/:id”, :controller =\> “categories”,  
:action  
=\> “show”

How your URLs look is always a product of the Routing system.

Cheers,

Andy

---

<div class="post-metadata">

**Author:** ![gregm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/bc79bd/32.png) [@gregm](https://www.ruby-forum.com/u/gregm)\
**Post date:** [April 19, 2010, 1:33pm UTC](https://www.ruby-forum.com/t/caching-with-rails/187178/5 "2010-04-19T13:33:10Z")

</div>

Andy J. wrote:

> > > [http://www.example.com/products/category/5](http://www.example.com/products/category/5)
> > > 
> > > Then it would work - the 5 would become 5.html in a folder called  
> > > category  
> > > in a folder called products.
> > 
> > Thank you Andy for you explanation.  
> > How do you do to make your urls look like this?
> 
> config/routes.rb
> 
> map.category “/products/category/:id”, :controller =\> “categories”,  
> :action  
> =\> “show”

Does the link\_tag will still look like this: link\_to “my link”,  
products\_path(:category\_id =\> 3) ?
