-1

I have these routes

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

The URL is http://localhost:3000/brokers/1/approve.

It gives error

Couldn't find Broker with 'id'=

The controller which loads the broker is

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

The parameters are

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

How can I use the path /brokers/:id/approve like how #update does so it can use the same method to load the model?

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

Chloe
  • 25,162
  • 40
  • 190
  • 357

2 Answers2

0

I used

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

yields

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

I had to change the path around, but that's fine.

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

Chloe
  • 25,162
  • 40
  • 190
  • 357
0

You could do this by updating your routes to

resources :brokers do
    member do
      patch :approve
    end
end

which would treat the nested route as a member route. This would create a route as follows:

approve_broker PATCH  /brokers/:id/approve(.:format) brokers#approve
Kirti Thorat
  • 52,578
  • 9
  • 101
  • 108