Rails URL 的嵌套路由和参数(最佳实践) [英] Nested Routes and Parameters for Rails URLs (Best Practice)

查看:62
本文介绍了Rails URL 的嵌套路由和参数(最佳实践)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对 RESTful url 和所有非嵌套 url 背后的理论有很好的理解,但我仍然不太确定这在企业应用程序中的外观,比如亚马逊、StackOverflow 或谷歌......

I have a decent understanding of RESTful urls and all the theory behind not nesting urls, but I'm still not quite sure how this looks in an enterprise application, like something like Amazon, StackOverflow, or Google...

Google 有这样的网址:

Google has urls like this:

亚马逊是这样的:

  • http://www.amazon.com/books-used-books-textbooks/b/ref=sa_menu_bo0?ie=UTF8&node=283155&pf_rd_p=328655101&pf_rd_s=left-nav-1&pf_rd_t=101&pf_rd_i=507846&pf_rd_m=ATVPDKIKX0DER&pf_rd_r=1PK4ZKN4YWJJ9B86ANC9
  • http://www.amazon.com/Ruby-Programming-Language-David-Flanagan/dp/0596516177/ref=sr_1_1?ie=UTF8&s=books&qid=1258755625&sr=1-1

像这样的 StackOverflow:

And StackOverflow like this:

所以我的问题是,为这些系统创建 url 的最佳实践是什么?你什么时候开始在 url 中存储参数,什么时候不?这些大公司似乎并没有遵循 ruby​​ 社区中如此激烈争论的规则(例如,你几乎不应该嵌套 URL),所以我想知道你如何在更大规模的项目中实现自己的 URL,因为它似乎不嵌套 url 的想法在任何比博客大的地方都失效了.

So my question is, what is best practice in terms of creating urls for systems like these? When do you start storing parameters in the url, when don't you? These big companies don't seem to be following the rules so hotly debated in the ruby community (that you should almost never nest URLs for example), so I'm wondering how you go about implementing your own urls in larger scale projects because it seems like the idea of not nesting urls breaks down at anything larger than a blog.

有什么建议吗?

推荐答案

不要被 Ruby 社区中的规则"所束缚.这个想法是,在嵌套 URL 时不应该过度,但是当它们合适时,它们被内置到 Rails 框架中是有原因的:使用它们.

Don't get too bogged down by the "rules" in the Ruby community. The idea is that you shouldn't go overboard when nesting URLs, but when they're appropriate they're built into the Rails framework for a reason: use them.

如果一个资源总是落入另一个资源,嵌套它.没有错.比一个更深的地方有时会有点痛苦,因为您的路线路径会很长并且可能会有点混乱.

If a resource always falls within another resource, nest it. Nothing wrong with that. Going deeper than one can sometimes be a bit of a pain because your route paths will be very long and can get a bit confusing.

另外,不要将嵌套与命名空间混淆.仅仅因为您看到 example.com/admin/products/1234/edit 并不意味着发生任何嵌套.当它们实际上不在代码级别时,路由会使它们看起来像是嵌套的.

Also, don't confuse nesting with namespacing. Just because you see example.com/admin/products/1234/edit does not mean that there's any nesting happening. Routing can make things look nested when they're actually not at the code level.

我个人是嵌套的忠实粉丝,并且在我的应用程序中经常使用它(只有一个级别——偶尔两个).此外,添加使用单词而不仅仅是 ID 的固定链接样式的 URL 更具视觉吸引力,并且它们可以帮助 SEO,无论它们是否嵌套.

I'm personally a big fan of nesting and use it often (just one level -- occasionally two) in my applications. Also, adding permalink style URLs that use words rather than just IDs are more visually appealing and they can help with SEO, whether or not they're nested.

这篇关于Rails URL 的嵌套路由和参数(最佳实践)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆