将新视图映射到现有资源...如何创建 /resource/number/foobar?
我构建了一个 Ruby on Rails (2.3.8) 应用程序,允许用户跟踪他们的锻炼情况。此外,如果用户是健身房所有者,他们可以在我的应用程序中创建健身房(在我的例子中称为 Box)。盒子将站点范围内的活动过滤为仅该盒子的成员(通过会员协会完成,但这对此并不重要)。
Box 位于 Boxes 资源中,如您所料,可以在我的应用程序中的 /boxes/14 中访问。我现在想为每个框创建一个 leaderboard
,理想情况下位于 /boxes/14/leaderboard
并仅显示框 14 中的活动(当然 14 是一个示例数字) )。排行榜不需要是完整的资源,因为我只是通过named_scopes 过滤活动。
出于某种原因,我以前没有遇到过这样的情况。在框架内实现这一目标的最佳方法是什么?我尝试在 boxes_controller
中创建一个方法,并将路由映射到 /boxes/leaderboard
但当然,它会轻松地查找 id = Leaderboard 的 Box。
有什么想法吗?或者我在这里遗漏了一些基本的东西。
I have built a Ruby on Rails (2.3.8) application that allows users to track their workouts. In addition, if a user is a gym owner, they can create a gym (in my case called a Box) in my app. A box filters site wide activity to just members of that box (accomplish through a membership association but that isn't important for this).
A Box lives in a Boxes resource and is accessed in my app at /boxes/14 as you would expect. I now want to create a leaderboard
for each box that would ideally live at /boxes/14/leaderboard
and only display activity from box 14 (of course 14 is an example number). The leaderboard doesn't need to be a full resource because I am simply filtering activity through named_scopes.
For some reason I haven't run into a situation like this before. What is the best way to accomplish this within the framework? I have tried creating a method in the boxes_controller
and mapping the route to /boxes/leaderboard
but of course that restfully looks for a Box with id = leaderboard.
Any ideas? Or am I missing something fundamental here.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为最简单的方法是为此场景添加成员路由。在你的routes.rb中添加类似的内容:
这将产生一个如下所示的附加路由:
并且该路由将被路由到BoxesController中名为leaderboard的操作。
I think the easiest way would be to add a member route for this scenario. Add something like this in your routes.rb:
That will result in an additional route looking like this:
And that will be routed to an action called leaderboard in the BoxesController.