【问题标题】:Appropriately nest resources in routes.rb在 routes.rb 中适当地嵌套资源
【发布时间】:2013-12-17 23:36:39
【问题描述】:

我的应用程序有深度嵌套的资源,但是在阅读http://guides.rubyonrails.org/routing.html#shallow-nesting 之后,我意识到拥有这些深度嵌套的资源是不明智的。这是我的路线目前所处的情况。

resources :assortments do
  resources :comments do
    member do
      post :like
      post :unlike
    end
  end
  member do
    post :like
    post :unlike
  end
  resources :designs do
    resources :comments do
      member do
        post :like
        post :unlike
      end
    end
    member do
      post :like
      post :unlike
      get :likes
    end
  end
end

我想知道组织此结构的正确方法,以使其不那么混乱和“正确”。或者我有什么好吗?
谢谢。

【问题讨论】:

    标签: ruby-on-rails ruby routes ruby-on-rails-4 nested-attributes


    【解决方案1】:

    您可以只在顶部的resources :assortments 上指定shallow: true,并且应该自动为所有嵌套路由构建浅层路由。

    我也会将:comments 资源转移到关注点中,有点像这样

    concern :commentable do
      resources :comments do
        member do
          :like
          :unlike
        end
      end
    end
    
    resources :assortments, shallow: true do
      concerns :commentable
      ...
      resources :designs do
        concerns :commentable
      end
    end
    

    在一个不相关的话题上,我会考虑让LikesController 拥有一个集中的点赞处理资源,当你遇到多态点赞时可能会让人头疼。

    【讨论】:

      猜你喜欢
      • 2013-06-17
      • 2012-08-23
      • 2013-06-25
      • 1970-01-01
      • 2011-05-24
      • 1970-01-01
      • 2011-01-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多