只是做一些常规的非休息路线
map.connect ":controller/:username", :action => :index
map.connect ":controller/:username/:id", :action => :show
这条路线
/photos/shpigford
致
index
对…采取行动
PhotosController
把
:username => "shpigford"
在
params
搞砸。第二行将使用
id
,就像
/videos/edgerunner/35467
致
show
对…采取行动
VideosController
把
:username => "edgerunner", :id => "35467"
在
参数
搞砸。
您可能需要稍微约束这些路由,以便它们不会意外地与其他类似路由匹配。您可能还需要添加命名路由。更详细的设置是:
map.with_options :controller => /photos|videos|blogs/,
:username => /[-a-z0-9]+/,
:conditions => { :method => :get } do |route|
route.user_assets ":controller/:username", :action => :index
route.user_asset ":controller/:username/:id", :action => :show, :id => /\d+/
end
这个
with_options
块将其选项应用于其中的所有路由定义,这样我们就不必每次都重复它们。这是怎么回事,
除了上面的基本设置
,是;
-
确保第一段是我们拥有的三个控制器之一(否则它很乐意接受
/zombies/fandango
并尝试执行不存在的索引操作。
ZombiesController
惨败)
-
确保第二段(
username
)只包含小写的ASCII字母、数字和破折号。
-
只接受HTTP GET请求。不欢迎发布、放置和删除。
-
将这些路由定义为
命名路由
所以你可以打电话
user_assets_path(:photos,'edgerunner')
在您要渲染的视图中
/photos/edgerunner
-
确保第三部分(
身份证件
)只有数字,所以不匹配
/photos/clouds/nimbus