使用嵌套路由 Rails 3 而不实际允许访问父资源
我正在使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你的方法 - 这是标准方法,没有比这更好的了。
Your approach - it's a standard approach, there is nothing better.
我可以想出几种方法来做到这一点。一种方法是您上面所做的。但是,似乎您不需要公开设备组控制器或其任何操作,因此以下操作应该很好:
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:
The
scope
block will append "/equipment_groups" to every route in it. This will essentially "fake" a nested route.