1

我有以下路线:

resources :businesses, only: [:show, :index, :new, :create] do
  resources :pledge_drives
end

这将创建以下内容:

% rake routes | grep pledge
     business_pledge_drives GET    /businesses/:business_id/pledge_drives(.:format)                                         pledge_drives#index
                            POST   /businesses/:business_id/pledge_drives(.:format)                                         pledge_drives#create
  new_business_pledge_drife GET    /businesses/:business_id/pledge_drives/new(.:format)                                     pledge_drives#new
 edit_business_pledge_drife GET    /businesses/:business_id/pledge_drives/:id/edit(.:format)                                pledge_drives#edit
      business_pledge_drife GET    /businesses/:business_id/pledge_drives/:id(.:format)                                     pledge_drives#show
                            PUT    /businesses/:business_id/pledge_drives/:id(.:format)                                     pledge_drives#update
                            DELETE /businesses/:business_id/pledge_drives/:id(.:format)                                     pledge_drives#destroy

希望调用 URL 帮助程序方法business_pledge_drife_path。如何纠正 Rails 的单数化drives?实际的 URL 很好。

编辑: 这个答案是正确的。我将以下内容添加到 config/initializers/inflections.rb:

ActiveSupport::Inflector.inflections do |inflect|
  inflect.plural 'drive', 'drives'
  inflect.singular 'drives', 'drive'
end

现在我的路线是:

     business_pledge_drives GET    /businesses/:business_id/pledge_drives(.:format)                                         pledge_drives#index
                            POST   /businesses/:business_id/pledge_drives(.:format)                                         pledge_drives#create
  new_business_pledge_drive GET    /businesses/:business_id/pledge_drives/new(.:format)                                     pledge_drives#new
 edit_business_pledge_drive GET    /businesses/:business_id/pledge_drives/:id/edit(.:format)                                pledge_drives#edit
      business_pledge_drive GET    /businesses/:business_id/pledge_drives/:id(.:format)                                     pledge_drives#show
                            PUT    /businesses/:business_id/pledge_drives/:id(.:format)                                     pledge_drives#update
                            DELETE /businesses/:business_id/pledge_drives/:id(.:format)                                     pledge_drives#destroy
4

1 回答 1

1

大多数时候,rails 知道单词的正确复数形式,例如:型号名称。但是,在其他情况下,您可以使用 Inflections 在 Rails 中定义正确的转换。

于 2013-04-26T20:44:28.203 回答