Github 喜欢 Rails 中的路由 [英] Github like routes in Rails
问题描述
在 rails 中使用类似链式路由的 github
我有类似的网址:
'localhost:3000/document_managers/[:module_name]'
'localhost:3000/document_managers/[:module_name]/1/2/3/.' # can be any level deep
这是它们的路由定义:
map.connect '/document_managers/:module',
:controller => "document_managers",
:action => :new_tree,
:module => ["A","B","C"]
map.connect '/docuemnt_managers/:module/*path',
:controller => "document_managers",
:action => "new_tree",
:module => ["A","B","C"]
问题来了:
除了从上面给出的数组,即("A","B","C") 就像在任何时候 URL 必须是类似
The idea that module name value can't be anything except from the given above array i.e("A","B","C") like at any time the URL must be something like
localhost:3000/document_managers/A/1
或
localhost:3000/document_managers/B/221/1
或
localhost:3000/document_managers/C/121/1
但事实并非如此localhost:3000/document_managers/D/121/1
被视为有效的 url即使D"不在列出的数组中,并且模块也被设置为 D上面
but that not the case even though
localhost:3000/document_managers/D/121/1
is treated as valid url
and module is set to D even though the "D" is not in listed array
above
我想要 URL localhost:3000/document_managers/A
如果额外参数不是,也重定向到相同的操作,即 new_tree在 URL 中提供的包含额外参数localhost:3000/document_managers/C/121/1
然后重定向 URL适当地到所需的控制器和动作,但如果 URL 只有包含路径,直到模块名称 Rails 返回 routesActionController::UnknownAction
我不知道为什么,因为我已经定义了控制器和动作.
I want the the URL localhost:3000/document_managers/A
to
also redirect to same action i.e new_tree if the extra parameter isn't
provided as in the URL contain extra parameters
localhost:3000/document_managers/C/121/1
then the URL is redirected
appropriately to the desired controller and action but if the URL only
contain the path until the module name the Rails return a routes
ActionController::UnknownAction
I don't know why as I have already
defined the controller and action.
推荐答案
在 Rails 3.1 中,您可以在路由文件中执行此操作以获得您想要的:
In Rails 3.1, you can do this in your routes file to get what you want:
match '/document_managers/:module',
:controller => "document_managers",
:action => :new_tree,
:constraints => {:module => /[ABC]/}
这篇关于Github 喜欢 Rails 中的路由的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!