jeudi 3 janvier 2019

Specify parameter name for additional resource actions

I have some nested resources specified in routes.rb

resources :installation, except: %i[index edit update show] do
     resources :configuration, shallow: true, except: %i[index show] 
end

which generate the folling routes:

installation_configuration_index POST   /installation/:installation_id/configuration(.:format)     configuration#create
  new_installation_configuration GET    /installation/:installation_id/configuration/new(.:format) configuration#new
              edit_configuration GET    /configuration/:id/edit(.:format)                          configuration#edit
                   configuration PATCH  /configuration/:id(.:format)                               configuration#update
                                 PUT    /configuration/:id(.:format)                               configuration#update
                                 DELETE /configuration/:id(.:format)                               configuration#destroy
              installation_index POST   /installation(.:format)                                    installation#create
                new_installation GET    /installation/new(.:format)                                installation#new
                    installation DELETE /installation/:id(.:format)                                installation#destroy

I would now like to add some additional actions to the configuration, such as enable,disable

resources :installation, except: %i[index edit update show] do
  resources :configuration, shallow: true, except: %i[index show] do
    post :enable
    post :disable
  end
end

whichs adds some the following:

 configuration_enable POST   /configuration/:configuration_id/enable(.:format)          configuration#enable
 configuration_disable POST   /configuration/:configuration_id/disable(.:format)         configuration#disable

This is fine, except for the fact that these new actions use the parameter :configuration_id instead of :id. This makes it a bit annoying to use before_actions that check for parameter validity across the whole controller.

I would like to end up something similar to the following:

 configuration_enable POST   /configuration/:id/enable(.:format)          configuration#enable
 configuration_disable POST   /configuration/:id/disable(.:format)         configuration#disable

I have already searched and found things like using param: :id or key: id, none of which had the desired effect. What works but is a bit messy is adding the new routes seperately like so:

post 'configuration/:id/enable', action: 'enable', as: 'configuration/enable', to: 'configuration#enable'
post 'configuration/:id/disable', action: 'disable', as: 'configuration/disable', to: 'configuration#disable'
resources :installation, except: %i[index edit update show] do
  resources :configuration, shallow: true, except: %i[index show]
end

I there a cleaner way to accomplish the same thing while still using nested resources?

Aucun commentaire:

Enregistrer un commentaire