Ruby on Rails:路由树的地方层次结构 [英] Ruby on Rails: Routing for a tree hierarchy of places
问题描述
因此,我们有一个遗留系统,该系统可以跟踪ID为欧洲/法国/巴黎的地方,而我正在构建一个Rails外观以将其转换为类似http:// foobar / places / Europe / France的URL /巴黎。这项要求是不可商议的,可能的级别数是无限的,并且我们不能逃脱斜线。
So we've got a legacy system that tracks places with IDs like "Europe/France/Paris", and I'm building a Rails facade to turn this into URLs like http:// foobar/places/Europe/France/Paris. This requirement is not negotiable, the number of possible levels in unlimited, and we can't escape the slashes.
为 http:// foobar / places / Europe 很简单:
map.resources :places
...但是http:// foobar /地方/欧洲/法国抱怨欧洲没有任何反应。我尝试过:
...but http:// foobar/places/Europe/France complains "No action responded to Europe". I tried:
map.connect '/places/:id', :controller => 'places', :action => 'show'
...但是这给出了相同的结果,因为:id显然在第一个'/'。如何使ID覆盖位置之后的所有内容?
...but this gives the same result, as apparently the :id ends at the first '/'. How do I make the ID cover anything and everything after the "places"?
推荐答案
请参阅《路由指南》以获取完整信息。文档:
Have a look at the Routing Guide for full documentation:
http://guides.rubyonrails。 org / routing.html
具体是 4.9 Route Globbing部分。
Specifically section "4.9 Route Globbing".
但我认为您真正想要做的就是声明您的路线,例如:
But I think what you really want to do is declare your route like:
map.connect '/places/*id', :controller => 'places', :action => 'index'
以类似URL的
/places/foo/bar/1
Yields a params[:id] => ["foo", "bar", "1"]
您可以轻松地(重新)加入用 /产生想要的完整字符串 foo / bar / 1(您可能必须手动重新插入前导斜杠。
Which you could easily (re)join with "/" to yield the full string you want "foo/bar/1" (you will probably have to re-insert the leading slash manually.
应该可以你去。
这篇关于Ruby on Rails:路由树的地方层次结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!