2014-08-28 80 views
0

在我的路线我有一个嵌套的资源,像这样:Rails的路线 - 嵌套单一的资源用:ID,而不是:资源名_ID

namespace :public, path: "/" do 
constraints(Subdomain::Public) do 
    namespace :v1 do 
    post "/webhooks/:id/test", to: "webhooks#test" 
    resources :webhooks,  only: [ :index, :show, :create, :update, :destroy ] 

的后网络挂接路线变为:

POST /v1/webhooks/:id/test(.:format) 

这很好。不过,我想打扫一下有点像这样:

namespace :public, path: "/" do 
constraints(Subdomain::Public) do 
    namespace :v1 do 
    resources :webhooks, only: [ :index, :show, :create, :update, :destroy ] do 
     post :test 
    end 

这导致这样的路线:

POST /v1/webhooks/:webhook_id/test(.:format) 

不好。我想在路径中使用常规:id。我在这里做错了什么?

回答

1
resources :webhooks, only: [ :index, :show, :create, :update, :destroy ] do 
    member do 
     post :test 
    end 
    end 
+0

这就是我一直在寻找的。 – cheeseandpepper 2014-08-28 16:17:52

0

这就是Rails路由器设置的方式。如果您不喜欢它生成的方式,您可以使用您喜欢的参数名称进行手动路由,就像您在第一个示例中所做的那样。

通常我会说这是最好的两个拥抱这里的公约,而不是贬低它。这只是参数的名称,对最终用户不可见。你违反约定的越多,你的代码越不规范,维护就越困难。