开发者

Rails will_paginate routing caching duplicate pages

开发者 https://www.devze.com 2022-12-13 06:47 出处:网络
I use the will_paginate plug-in. In oder to generate routes that I can cache ( /posts/index/2 instead of /posts?page=2) I added the following to my routes.rb:

I use the will_paginate plug-in.

In oder to generate routes that I can cache ( /posts/index/2 instead of /posts?page=2) I added the following to my routes.rb:

map.connect '/posts/index/1', :controller => 'redirect', :url => '/posts/'  

map.connect 'posts/index/:page',
          :controller => 'posts',
          :action => 'index',
          :requirements => {:page => /\d+/ },
          :page => nil

The first line redirects /posts/index/1 to /posts/ using a redirect controller, to avoid having a duplicate page.

Is there something wrong with the way I set up the 'posts/index/:page' rule?

I thought adding :requirements => {:page => /\d+/ } would ensure that /post/index/ without a :page parameter should not work, but /posts/index.html is getting cached.

How can I redirect /posts/index/ to /posts/ to avoid having both /posts.html and /posts/index.html ?

Thanks


UPDATE

I simply added

map.connect '/posts/index/', :controller => 'redirect', :url => '/posts/'

And I'm not getting duplicate pages anymore.

However, I still don't uderstand why I was getting /posts/index.html. Any explanations or suggestions on how to make this rule more succinct are welcome ;)!

map.connect '/posts/index/1', :controller => 'redirect', :url => '/posts/'  
map.connect '/posts/index/', :controller => 'redirect', :url => '/posts/'
map.connect 'posts/index/:page',
          :controller => 'posts',
          :action => 'index',
          :requirements => {:page =&开发者_如何学运维gt; /\d+/ },
          :page => nil


Here I found possible answer to your question.

I think that adding :page => nil can override previous condition. So maybe when you remove this part, it will work as you expected.

0

精彩评论

暂无评论...
验证码 换一张
取 消