简体   繁体   中英

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

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

A more clean way is to use the member

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM