具有嵌套资源的命名路由 [英] Named route with nested resources

查看:68
本文介绍了具有嵌套资源的命名路由的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在此处创建一条名为 have_many的其他命名路线。

I'm trying to make a named route 'have_many' other named routes here. But the way I'm doing it's not working.

这是我的问题:
我想通过/:platform_name访问多个游戏平台/。这是可行的:

Here is my problem: I've several game platforms that I want to access by /:platform_name/. This is working:

map.platform ':platform_name', 
              :controller => :platforms, 
              :action => :index,
              :platform_name => /pc|ps2|ps3|wii|ds|psp|xbox360/

但是我也有游戏每个平台,我都想按名称进行引用,因此我尝试了以下操作:

But I also have games inside each platform, that I want to refer by name, so I've tried:

map.platform ':platform_name', 
                  :controller => :platforms, 
                  :action => :index,
                  :platform_name => /pc|ps2|ps3|wii|ds|psp|xbox360/ do |platform|

   platform.games ':game_name',
                  :controller => :games
end

但是当我这样做时,甚至平台路线也停止工作。
是否可以在其他命名路由中包含一个命名路由?
我只能想象没有has_many关系的肮脏代码可以实现这一点。欢迎任何想法:)

But when I do this, even the platform route stop working. Is it possible to have a named route inside other named route? I can only imagine a dirty code to achieve this without the has_many relation. Any idea is welcome :)

推荐答案

我不确定这是否是您想要的,但是如何通过2条命名路由嵌套?

I'm not sure if this is what you want but what about nesting through 2 named routes?

map.platform ':platform_name', :controller => :platforms, :action => :index

map.games ':platform_name/:game_name', :controller => :games, :action => :show

这篇关于具有嵌套资源的命名路由的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆