Rails Rails 4.0 路由可以复用了。routing_concerns

xds2000 · 2012年08月14日 · 最后由 _samqiu 回复于 2012年08月14日 · 2879 次阅读

https://github.com/rails/rails/compare/fa736e69a197...2d9dbf416b14

例子: Code before:

resources :messages do
  resources :comments
end

resources :posts do
  resources :comments
  resources :images, only: :index
end

Code after:

concern :commentable do
  resources :comments
end

concern :image_attachable do
  resources :images, only: :index
end

resources :messages, concerns: :commentable

resources :posts, concerns: [:commentable, :image_attachable]

减少冗余,更加精练了。

不过感觉可读性会差些

个人觉得这种比较适合 polymorphic 类型比较多的应用。

需要 登录 后方可回复, 如果你还没有账号请 注册新账号