# Problem with route defaults and caching

**URL:** <https://www.ruby-forum.com/t/problem-with-route-defaults-and-caching/120939>\
**Category:** Rails\
**Created:** [November 21, 2007, 10:30pm UTC](https://www.ruby-forum.com/t/problem-with-route-defaults-and-caching/120939 "2007-11-21T22:30:56Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![andrewkendall](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/b77776/32.png) [@andrewkendall](https://www.ruby-forum.com/u/andrewkendall)\
**Post date:** [November 21, 2007, 10:30pm UTC](https://www.ruby-forum.com/t/problem-with-route-defaults-and-caching/120939/1 "2007-11-21T22:30:56Z")

</div>

Hi,

I’m having a problem with routes and caching.

I have two routes set up as follows in my route file…

map.connect ‘stats’, :controller =\> ‘stats’, :action =\> ‘index’  
map.connect ‘stats/:type’, :controller =\> ‘stats’, :action =\> ‘index’,  
:defaults =\> {:type =\> nil}

When I use cache\_action, the :type is never added to the url for  
caching. All the cached names are simply ‘/stats’ even if a :type is  
defined.

If I remove the first route, leaving just this…

map.connect ‘stats/:type’, :controller =\> ‘stats’, :action =\> ‘index’,  
:defaults =\> {:type =\> nil}

my cached names now use the :type properly… ‘/stats/stat\_type’

But now the problem is that any links I have in my view for my stats  
controller always add the :type even if I didn’t specify it.

So if I load the page with the url ‘stats/stat\_type’, urls generated for  
the stats controller append the :type even if I didn’t specify it.

Ex.) link\_to ‘test’, :action =\> ‘index’ results in ‘stats/stat\_type’

I don’t want the :type included if I didn’t specify it. Does anybody  
have any idea what’s wrong with my routes?

Thanks,  
Andrew

---

<div class="post-metadata">

**Author:** ![andrewkendall](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/b77776/32.png) [@andrewkendall](https://www.ruby-forum.com/u/andrewkendall)\
**Post date:** [November 22, 2007, 1:12am UTC](https://www.ruby-forum.com/t/problem-with-route-defaults-and-caching/120939/2 "2007-11-22T01:12:30Z")

</div>

Turns out I need to pass :type =\> nil to link\_to in order to force the  
value… simply leaving it out isn’t good enough.
