开发者

Rails 3 global and optional route for format to assist page caching

I would like to page cache just about everything in my API, so instead of the .format or ?format= way that is usually called, I want to make the final url param hardcoded into the rou开发者_StackOverflow社区te. Examples:

/sounds/123/xml
/sounds/123/json
/sounds/123/whats_new/xml
/sounds/xml
/sounds/blah_method/xml

I know I can use named and matched routes to add this to EVERY call, but that is not very DRY and seems like there should be a better rails way. Is there any way to accomplish this?


You can hook into the route recognition or generation with the routing-filter plugin. It is very flexible, take a look.

For the generation, your use case would be to check if there's a params[:format] and in that case add a new segment at the end of the url.

If you also need recognition, this time it's the opposite, check if there's a trailing segment with either '/xml' or '/json'and in that case add params[:format]to match it.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜