# REST + Pagination + Caching

**URL:** <https://www.ruby-forum.com/t/rest-pagination-caching/122330>\
**Category:** Rails\
**Created:** [December 6, 2007, 6:06pm UTC](https://www.ruby-forum.com/t/rest-pagination-caching/122330 "2007-12-06T18:06:58Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![kayros](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/45deac/32.png) [@kayros](https://www.ruby-forum.com/u/kayros)\
**Post date:** [December 6, 2007, 6:06pm UTC](https://www.ruby-forum.com/t/rest-pagination-caching/122330/1 "2007-12-06T18:06:58Z")

</div>

Hi all,

I want to have a RESTful resource (because I need to use  
atom\_feed\_builder).

For the sake of the exemple, let say I need : map.resources :news

I want to be able to list all the news in index action, paginate it  
(using will\_paginate for example) and add cache to it each paginated  
page.

To add cache I need to have url like news/page/2, urls like  
news/?page=2 can’t be cached page by rails.

So any idea to solve this problem ?

## Thanks.

Nicolas C.

---

<div class="post-metadata">

**Author:** ![kayros](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/45deac/32.png) [@kayros](https://www.ruby-forum.com/u/kayros)\
**Post date:** [December 7, 2007, 9:36am UTC](https://www.ruby-forum.com/t/rest-pagination-caching/122330/2 "2007-12-07T09:36:12Z")

</div>

2007/12/6, Nicolas C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> To add cache I need to have url like news/page/2, urls like  
> news/?page=2 can’t be cached page by rails.
> 
> So any idea to solve this problem ?

The answer was “use fragment caching”

# Controller

def index  
unless read\_fragment({:page =\> params[:page] || 1}) # Add the  
page param to the cache naming  
@news = News.paginate :all, :page =\> params[:page]  
end  
end

# View

## \<% cache ({:page =\> params[:page] || 1}) do %\> … All of the html to display the posts … \<% end %\>

Nicolas C.
