Rails路由:更改资源标识符路径名称?总是使用params [:model_id]而不是params [:id]

时间:2018-04-20 15:37:16

标签: ruby-on-rails

我不确定如何提出这个问题......

但我正在使用嵌套的ROUTES,如下所示。

我想知道使用params[:business_id]始终可以找到商家ID ,显然除了要求我使用params[:id]的实际业务控制器之外。

有没有办法将路径资源ID参数更改为始终为:business_id,而不必像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

1 个答案:

答案 0 :(得分:2)

如果您使用的是Rails 4+,则可以使用param方法中的resources选项执行此操作

Overriding Route Parameters

假设您的代码看起来像

resources :businesses

您可以添加一个参数

resources :businesses, param: :business_id

哪个应该生成路线

                    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)

<强>更新

由于您使用相同的嵌套资源生成这些路由,因此您必须执行以下操作

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