使用嵌套路由 Rails 3 而不实际允许访问父资源

发布于 2024-10-23 17:53:43 字数 365 浏览 2 评论 0原文

我正在使用 Rails 3,并且有两个模型 EquipmentGroup 和 Reservation。我希望预留成为设备组的嵌套资源,以便我可以使用以下 URL 访问它们:

/equipment_groups/:equipment_group_id/reservations/:id

但是,我不想为设备组创建路由。我可以通过以下方式实现这一点,但这似乎是一个黑客:

resources :equipment_groups, :only => [] do
  resources :reservations
end

有更好的方法来做到这一点吗?我似乎无法在文档中轻松找到答案。

I'm using Rails 3 and I have two models EquipmentGroup and Reservation. I want reservations to be a nested resource of equipment groups so that I can access them with URLs like:

/equipment_groups/:equipment_group_id/reservations/:id

However, I don't want to create routes for the equipment groups. I can achieve this through the following, but it seems like a hack:

resources :equipment_groups, :only => [] do
  resources :reservations
end

Is there a better way to do this? I can't seem to find an answer easily in the documentation.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

思念绕指尖 2024-10-30 17:53:43

你的方法 - 这是标准方法,没有比这更好的了。

Your approach - it's a standard approach, there is nothing better.

┾廆蒐ゝ 2024-10-30 17:53:43

我可以想出几种方法来做到这一点。一种方法是您上面所做的。但是,似乎您不需要公开设备组控制器或其任何操作,因此以下操作应该很好:

scope "/equipment_groups" do
    resources :reservations
end

scope 块会将“/equipment_groups”附加到其中的每个路由。这本质上是“伪造”一个嵌套路由。

I can think of a few ways of doing this. One way is what you've done above. However, it seems like you have no need to expose the equipment groups controller or any of its actions, so the following should do just fine:

scope "/equipment_groups" do
    resources :reservations
end

The scope block will append "/equipment_groups" to every route in it. This will essentially "fake" a nested route.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文