【问题标题】:Rails Routes - Nested single resource with :id, NOT :resource-name_idRails Routes - 嵌套单个资源:id,NOT:resource-name_id
【发布时间】:2014-08-28 15:55:24
【问题描述】:

在我的路线中,我有一个这样的嵌套资源:

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 webhook 路由变成:

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 在路径中。我在这里做错了什么?

【问题讨论】:

    标签: ruby-on-rails routes


    【解决方案1】:
      resources :webhooks, only: [ :index, :show, :create, :update, :destroy ] do
        member do
          post :test
        end
      end
    

    【讨论】:

    • 这就是我要找的。​​span>
    【解决方案2】:

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

    一般来说,我会说最好是两个人在这里接受约定,而不是违背约定。这只是参数的名称,对最终用户不可见。你越是打破惯例,你的代码就越不标准,也就越难维护。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多