将新视图映射到现有资源...如何创建 /resource/number/foobar?

发布于 2024-10-07 19:56:09 字数 552 浏览 2 评论 0原文

我构建了一个 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 技术交流群。

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

发布评论

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

评论(1

裸钻 2024-10-14 19:56:09

我认为最简单的方法是为此场景添加成员路由。在你的routes.rb中添加类似的内容:

map.resources :boxes, :member => { :leaderboard => :get }

这将产生一个如下所示的附加路由:

leaderboard_box GET   /boxes/:id/leaderboard    {:controller=>"boxes", :action=>"leaderboard"}

并且该路由将被路由到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:

map.resources :boxes, :member => { :leaderboard => :get }

That will result in an additional route looking like this:

leaderboard_box GET   /boxes/:id/leaderboard    {:controller=>"boxes", :action=>"leaderboard"}

And that will be routed to an action called leaderboard in the BoxesController.

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