路由参数约束保持格式不被正确检测

时间:2011-12-22 12:52:14

标签: ruby-on-rails ruby-on-rails-3

Rails 3.1.3

# routes.rb
get "posts/:id" => "posts#show", :constraints => { :id => /\d.+/ }

在rails控制台中

> Rails.application.routes.recognize_path("posts/101-haha.js")
=> {:controller=>"posts", :action=>"show", :id=>"101-haha.js"}

我的rake routes给出了

GET      /posts/:id(.:format)

因为.:format似乎是隐含的,为什么不能正确检测格式呢?是因为我的约束太贪婪了吗?

1 个答案:

答案 0 :(得分:0)

/\d.+/正在吃点“。”在“.js”中 试着逃避它:

get "posts/:id" => "posts#show", :constraints => { :id => /\d([^\.].)+/ }

http://rubular.com/r/ecqCnnYBpK