Ruby on rails 使用浅化正确配置嵌套资源

Ruby on rails 使用浅化正确配置嵌套资源,ruby-on-rails,Ruby On Rails,我在嵌套资源和文档中遇到了我的第一个问题 我无法完全理解如何理解这一点,以及如何将其正确地应用于我的情况,目前我的东西是这样设置的: resources :stores do resources :locations do resources :business_hours end end resources :stores do resources :locations, shallow: true end resources :locatio

我在嵌套资源和文档中遇到了我的第一个问题

我无法完全理解如何理解这一点,以及如何将其正确地应用于我的情况,目前我的东西是这样设置的:

  resources :stores do
    resources :locations do
      resources :business_hours
    end
  end
resources :stores do
    resources :locations, shallow: true
end

resources :locations do
    resources :business_hours
end
                    Prefix Verb   URI Pattern                                          Controller#Action
   location_business_hours GET    /locations/:location_id/business_hours(.:format)     business_hours#index
             business_hour GET    /business_hours/:id(.:format)                        business_hours#show
           store_locations GET    /stores/:store_id/locations(.:format)                locations#index
                  location GET    /locations/:id(.:format)                             locations#show
                    stores GET    /stores(.:format)                                    stores#index
                     store GET    /stores/:id(.:format)                                stores#show

现在我想限制嵌套,他们推荐的方式,但我不确定如何实现这一点,因为地点属于商店,营业时间属于地点

rails文档本质上所说的是,在上面的资源配置中,您将有一个url,在您的网页上会出现类似的内容

mywebapplication.com/stores/1/locations/1/business\u hours/1

使用代码对应的rails帮助器方法

stores\u locations\u business\u hours\u url

现在这并没有什么问题,你可以这样做,但你会开始遇到麻烦,尤其是你的营业时间控制器。原因是,对于控制器,您必须传入以下内容之前的每个@model对象。你必须做一些类似的事情

stores\u locations\u business\u hours\u url([@store,@location,@business\u hour])

访问一个页面。要限制这一点,您需要执行以下操作:

  resources :stores do
    resources :locations do
      resources :business_hours
    end
  end
resources :stores do
    resources :locations, shallow: true
end

resources :locations do
    resources :business_hours
end
                    Prefix Verb   URI Pattern                                          Controller#Action
   location_business_hours GET    /locations/:location_id/business_hours(.:format)     business_hours#index
             business_hour GET    /business_hours/:id(.:format)                        business_hours#show
           store_locations GET    /stores/:store_id/locations(.:format)                locations#index
                  location GET    /locations/:id(.:format)                             locations#show
                    stores GET    /stores(.:format)                                    stores#index
                     store GET    /stores/:id(.:format)                                stores#show

因此,现在不再是
mywebapplication.com/stores/1/locations/1
了,url将是这样的
mywebapplication.com/locations/1
,而现在您的营业时间url将是一级深度。这就是文档的含义

按照Rails的要求,只需向每个嵌套的
资源添加
shallow:true

resources :stores do
  resources :locations, shallow: true do
    resources :business_hours, shallow: true
  end
end
正如他们所说,这会产生“具有最小信息量以唯一标识资源的路由”,如下所示:

  resources :stores do
    resources :locations do
      resources :business_hours
    end
  end
resources :stores do
    resources :locations, shallow: true
end

resources :locations do
    resources :business_hours
end
                    Prefix Verb   URI Pattern                                          Controller#Action
   location_business_hours GET    /locations/:location_id/business_hours(.:format)     business_hours#index
             business_hour GET    /business_hours/:id(.:format)                        business_hours#show
           store_locations GET    /stores/:store_id/locations(.:format)                locations#index
                  location GET    /locations/:id(.:format)                             locations#show
                    stores GET    /stores(.:format)                                    stores#index
                     store GET    /stores/:id(.:format)                                stores#show
位置
的收集操作(例如
索引
)嵌套在
存储
下,因为
位置
属于
存储
,但为了标识特定位置,路线引用
位置/1
,不带
存储
前缀,因为您不需要
存储
ID来标识
位置

这从树上级联而下:要识别
营业时间
收集操作,您需要时间所属的
位置
,但由于您有
位置
ID,因此不需要涉及
存储
,因此您可以获得
位置/:ID/营业时间
。当您需要一组特定的小时数时,您不再需要
位置
,因此只需要
/business\u hours/1

如果要维护小时收集路径(即,
/stores/1/location/2/business\u hours
)的整个层次结构,则需要在
/stores/1/locations/2
路径下保留其成员操作(
show
edit
,等等),或者,您需要使用较少的Rails帮助程序手动指定所需的路径