我如何更改为使用与更新相同的路由 :id 而不是 :model_id?

How do I change to use the same route :id as update instead of :model_id?

我有这些路线

  resources :brokers do
    patch :approve
rake routes
        broker_approve PATCH  /brokers/:broker_id/approve(.:format)       brokers#approve
                       PATCH  /brokers/:id(.:format)                      brokers#update

URL 是 http://localhost:3000/brokers/1/approve

报错

Couldn't find Broker with 'id'=

加载代理的控制器是

def set_broker
  @broker = Broker.find(params[:id])

参数为

{"_method"=>"patch", "authenticity_token"=>"O8jztBqgRPcepes/p6IQqTfUQ==", "broker_id"=>"1"}

如何像 #update 那样使用路径 /brokers/:id/approve 以便它可以使用相同的方法加载模型?

指南:http://guides.rubyonrails.org/routing.html#nested-resources

我用过

  resources :brokers do
    patch :approve, on: :member

产量

>rake routes | grep approve
            approve_broker PATCH  /brokers/:id/approve(.:format)              brokers#approve

我不得不改变周围的路径,但没关系。

http://guides.rubyonrails.org/routing.html#adding-more-restful-actions

您可以通过将路线更新为

来做到这一点
resources :brokers do
    member do
      patch :approve
    end
end

这会将嵌套路由视为成员路由。这将创建如下路线:

approve_broker PATCH  /brokers/:id/approve(.:format) brokers#approve