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

Posted

技术标签:

【中文标题】Rails Routes:更改资源标识符路径名?总是使用 params[:model_id] 而不是 params[:id]【英文标题】:Rails Routes: change resource identifier path name? always use params[:model_id] instead of params[:id] 【发布时间】:2018-10-01 09:12:12 【问题描述】:

我不知道怎么问这个问题...

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

我喜欢知道 Business ID 总是可以使用params[:business_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

【问题讨论】:

我已经为您的问题添加了答案,但是,作为旁注,您应该问自己为什么要摆脱 Rails 的默认 :id 作为后者的动态部分路线。 我想在 BUSINESS 级别共享一个***身份验证控制器,并且不必在 params[:id]params[:business_id] 之间来回切换 【参考方案1】:

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

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

【讨论】:

你好 @leo 这不起作用,因为嵌套路由将有 params[:business_business_id] 此时您必须分离嵌套资源并复制resources 路由。如果您展示路线的布局方式会更有帮助。 BINGO - 我在任何地方都没有见过这个。你赢了互联网积分!【参考方案2】:

更干净的方法是使用member

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

【讨论】:

以上是关于Rails Routes:更改资源标识符路径名?总是使用 params[:model_id] 而不是 params[:id]的主要内容,如果未能解决你的问题,请参考以下文章

要映射到 rails 4 中的自定义路径的资源路由

Learn Rails5.2 Routes摘录

如何在 Rails 中找到方法来自的模块/类?

我可以通过资源方法在 Ruby on Rails 应用程序中使用排除项设置路由吗?

Rails引擎:命名空间多态URL

Rails 识别未知的 url 参数