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

Asked

Viewed 87 times

2

My route scheme 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 is used :dispute_id. To solve this I made the manual addition of 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'

Which in fact did not look elegant. Now, it is possible to solve this without the need to manually write each put?

1 answer

3


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

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.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.