White Space


Shallow Route Nesting

Text

Shallow Route Nesting 是 rails 2.2 增加的一个蛮有用的功能,具体的可以查看这里

你看到下面这样的链接后是什么样的感受?

http://example.org/publishers/1/magazines/2/photos/3

因为 Shallow Route Nesting 的出现,事情就变得简单多了,只需要在你的 route 的 resources 中加上 :shallow => true 就可以了,你就可以得到下面的链接了:

http://example.org/publishers/1
http://example.org/magazines/2
http://example.org/photos/3

明显赏心悦目多了,不过刚在 rails 3 中测试这个功能时发现,竟然无效,我无语了,在网上转了圈,找到这个 Missing shallow routes in new router DSL,看的我是莫名其妙啊,似乎 dhh 和 wycats 都不知道是怎么回事,好搞笑。

近而我又发现了 Rails Routing from the Outside In 这个页面前几天改动了不少,是由 wycats 改的,他把关于 Shallow Route Nesting 的部分整个删除了,加上了这么一句话:

Resources should never be nested more than 1 level deep.

同时还推荐去阅读这篇文章,也就是推荐下面的方式:

  resources :publishers do
    resources :magazines
  end
 
  resources :magazines do
    resources :photos
  end
 
  resources :photos

到此差不多可以确认 shallow 在 rails 3 中被去掉了,但我翻了好久的 changelog 和 commit 都没找到与此相关的内容,晕死。

不过 rails core 这么做,也是有道理的,确实符合 rails 和 resources 的哲学,相比使用 shallow,上面的代码虽然增加了不少,但明显更加的一目了然。



April 16, 2010, 7:21pm

  1. roamlog posted this