开发者

Mapping a new view to an existing resource...how do I create /resource/number/foobar?

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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜