Rails 3 全局和可选格式路由以协助页面缓存
我想对 API 中的所有内容进行页面缓存,因此我不想使用通常调用的 .format
或 ?format=
方式,而是制作最终的 url参数硬编码到路线中。示例:
/sounds/123/xml
/sounds/123/json
/sounds/123/whats_new/xml
/sounds/xml
/sounds/blah_method/xml
我知道我可以使用命名和匹配的路由将其添加到每个调用中,但这不是很DRY
,而且似乎应该有更好的 Rails 方式。有什么办法可以做到这一点吗?
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 route. 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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用 routing-filter 插件来连接路由识别或生成。非常灵活,快来看看吧。
对于生成,您的用例是检查是否有
params[:format]
,在这种情况下,在 url 末尾添加一个新段。如果您还需要识别,这次则相反,检查是否有带有
'/xml'
或'/json'
的尾随段,在这种情况下添加params[:format]
来匹配它。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 addparams[:format]
to match it.