Rubyist

Rails 3 resource routes with dots

Rails的路由一直一来就把dot (“.”)和slash (“/”)当作分隔符。一个简单的例子:

config/route.rb:

resources :tags

Rails console:

>> app.tag_path("a")
=> "/tags/a"
>> app.tag_path("a.rb")
ActionController::RoutingError: No route matches {:action=>"show", :controller=>"tags", :id=>"a.rb"}

解决办法就是在路由参数加上constraints选项:

resources :tags, :constraints => /\.*/

Reloading…
=> true
>> app.tag_path("a")
=> "/tags/a"
>> app.tag_path("a.rb")
=> "/tags/a.rb"

同样道理,斜杠的情况也一样。Rails2只需要把constraints换成requirements。

参考资源:

标签 Rails3, route, dots, slash

(使用BBCodeTextileGist)