Blair Anderson
Blair Anderson

Reputation: 20171

Rails Routes: change resource identifier path name? always use params[:model_id] instead of params[:id]

I'm not sure how to ask this question...

But i'm working with nested ROUTES as shown below.

I like knowing that the Business ID can always be found using params[:business_id], obviously except for the actual business controller which requires me to use params[:id].

Is there a way to change the route resource id param to always be :business_id instead of having to be like Business.find(params.values_at(:business_id, :id).first)??

                  business_exports GET    /businesses/:business_id/exports(.:format)                                                                                businesses/exports#index
                                   POST   /businesses/:business_id/exports(.:format)                                                                                businesses/exports#create
               new_business_export GET    /businesses/:business_id/exports/new(.:format)                                                                            businesses/exports#new
              edit_business_export GET    /businesses/:business_id/exports/:id/edit(.:format)                                                                       businesses/exports#edit
                   business_export GET    /businesses/:business_id/exports/:id(.:format)                                                                            businesses/exports#show
                                   PATCH  /businesses/:business_id/exports/:id(.:format)                                                                            businesses/exports#update
                                   PUT    /businesses/:business_id/exports/:id(.:format)                                                                            businesses/exports#update
                                   DELETE /businesses/:business_id/exports/:id(.:format)                                                                            businesses/exports#destroy
           business_replenishments GET    /businesses/:business_id/replenishments(.:format)                                                                         businesses/replenishments#index
             business_offer_prices GET    /businesses/:business_id/offer_prices(.:format)                                                                           businesses/offer_prices#index
                                   POST   /businesses/:business_id/offer_prices(.:format)                                                                           businesses/offer_prices#create
       business_unmatched_listings GET    /businesses/:business_id/unmatched_listings(.:format)                                                                     businesses/unmatched_listings#index
                                   POST   /businesses/:business_id/unmatched_listings(.:format)                                                                     businesses/unmatched_listings#create
      business_profit_loss_reports GET    /businesses/:business_id/profit_loss_reports(.:format)                                                                    businesses/profit_loss_reports#index
                        businesses GET    /businesses(.:format)                                                                                                     businesses#index
                                   POST   /businesses(.:format)                                                                                                     businesses#create
                      new_business GET    /businesses/new(.:format)                                                                                                 businesses#new
                     edit_business GET    /businesses/:id/edit(.:format)                                                                                            businesses#edit
                          business GET    /businesses/:id(.:format)                                                                                                 businesses#show
                                   PATCH  /businesses/:id(.:format)                                                                                                 businesses#update
                                   PUT    /businesses/:id(.:format)                                                                                                 businesses#update
                                   DELETE /businesses/:id(.:format)                                                                                                 businesses#destroy

Upvotes: 4

Views: 1585

Answers (2)

Jarl
Jarl

Reputation: 2861

A more clean way is to use the member

resources :businesses, param: :business_id do
  member do
    resources :exports
    ...
  end
end

Upvotes: 2

Leo Correa
Leo Correa

Reputation: 19809

If you're using Rails 4+ you can do this using the param option in the resources method

Overriding Route Parameters

Assuming you have code that looks like

resources :businesses

You can add an argument as such

resources :businesses, param: :business_id

Which should generate routes

                    businesses GET    /businesses(.:format)                                                                                                     businesses#index
                               POST   /businesses(.:format)                                                                                                     businesses#create
                  new_business GET    /businesses/new(.:format)                                                                                                 businesses#new
                 edit_business GET    /businesses/:business_id/edit(.:format)                                                                                            businesses#edit
                      business GET    /businesses/:business_id(.:format)                                                                                                 businesses#show
                               PATCH  /businesses/:business_id(.:format)                                                                                                 businesses#update
                               PUT    /businesses/:business_id(.:format)                                                                                                 businesses#update
                               DELETE /businesses/:business_id(.:format)

UPDATE

Since you're generating these routes using the same nested resources you'll have to do the following

resources :businesses, param: :business_id 
resources :businesses, only: [] do
  resources :exports
  ...
end

Upvotes: 4

Related Questions