我想在路徑中使用UUID id標準ID。這個工程:Rails 3與約束條件的嵌套路由
# UUIDs are used for ids
UUID_regex = /([a-z0-9]){8}-([a-z0-9]){4}-([a-z0-9]){4}-([a-z0-9]){4}-([a-z0-9]){12}/
resources :posts, :only => [:index, :show, :create], :constraints => {:id => UUID_regex}
這就是Rails接受/posts/fb0f7c67-6f9b-4e2c-a26b-7700bb9b334d
罰款。
當我開始嵌套他們這個樣子,不過,
# UUIDs are used for ids
UUID_regex = /([a-z0-9]){8}-([a-z0-9]){4}-([a-z0-9]){4}-([a-z0-9]){4}-([a-z0-9]){12}/
resources :posts, :only => [:index, :show, :create], :constraints => {:id => UUID_regex} do
resources :comments, :only => [:create, :destroy], :constraints => {:id => UUID_regex}
end
的Rails開始抱怨:No route matches [POST] "/post/fb0f7c67-6f9b-4e2c-a26b-7700bb9b334d/comments"
我缺少什麼?
Thx提前。
筆記:我在軌道3.2.2和紅寶石1.9.3; rake routes
是:
post_comments POST /posts/:post_id/comments(.:format) comments#create {:id=>/([a-z0-9]){8}-([a-z0-9]){4}-([a-z0-9]){4}-([a-z0-9]){4}-([a-z0-9]){12}/, :post_id=>/([a-z0-9]){8}-([a-z0-9]){4}-([a-z0-9]){4}-([a-z0-9]){4}-([a-z0-9]){12}/}
post_comment DELETE /posts/:post_id/comments/:id(.:format) comments#destroy {:id=>/([a-z0-9]){8}-([a-z0-9]){4}-([a-z0-9]){4}-([a-z0-9]){4}-([a-z0-9]){12}/, :post_id=>/([a-z0-9]){8}-([a-z0-9]){4}-([a-z0-9]){4}-([a-z0-9]){4}-([a-z0-9]){12}/}
posts GET /posts(.:format) posts#index {:id=>/([a-z0-9]){8}-([a-z0-9]){4}-([a-z0-9]){4}-([a-z0-9]){4}-([a-z0-9]){12}/}
POST /posts(.:format) posts#create {:id=>/([a-z0-9]){8}-([a-z0-9]){4}-([a-z0-9]){4}-([a-z0-9]){4}-([a-z0-9]){12}/}
post GET /posts/:id(.:format) posts#show {:id=>/([a-z0-9]){8}-([a-z0-9]){4}-([a-z0-9]){4}-([a-z0-9]){4}-([a-z0-9]){12}/}
謝謝你,約翰,你說得對,是這樣工作的。我的理解是一樣的 - 差不多。我相信,在嵌套的情況下父親約束被設置爲父親ID(唯一):所以我認爲你的設置會採取/職位/ [適當的uuid] /評論/:ID(在我的例子中爲摧毀),而是的意向/帖子/ [適當的uuid] /評論/ [適當的uuid]格式。它對我有意義,但我錯了。我無法在文檔中找到任何對嵌套情況的引用,順便說一句。 – fastcatch
是的,我從來沒有在文檔中看到過,這是一個恥辱。當別人遇到同樣的問題並正在嘗試時,我只會遇到它。 – JohnMetta