How do I force a nested route to use the same parameter name as the parent route?

2

My route schema needs to add 3 new routes to custom actions in my controller: start , cancel and finish .

resources :disputes do
  scope module: :dispute, except: :index do
    resources :conference, shallow: true, shallow_prefix: :dispute do
      put :start
      put :cancel
      put :finish
    end
  end
end

The problem is that instead of using :id , :dispute_id is used. To resolve this, I manually added each route:

resources :disputes do
  scope module: :dispute, except: :index do
    resources :conference, shallow: true, shallow_prefix: :dispute
  end
end

# REVIEW: Existe uma melhor maneira para fazer isso?
put 'conference/:id/start', to: 'dispute/conference#start'
put 'conference/:id/cancel', to: 'dispute/conference#cancel'
put 'conference/:id/finish', to: 'dispute/conference#finish'

What did not really look stylish. Now, is it possible to resolve this without the need to manually write each put ?

    
asked by anonymous 03.01.2017 / 21:25

1 answer

3

The solution is simple and is in the own route documentation . Just add the call to on: :member option:

resources :disputes do
  scope module: :dispute, except: :index do
    resources :conference, shallow: true, shallow_prefix: :dispute do
      put :start, on: :member
      put :cancel, on: :member
      put :finish, on: :member
    end
  end
end

This will inform Rails that the route is part (member) of the parent resource.

    
03.01.2017 / 21:46