Rails 默认路由中的路由名称生成不一致

use*_*014 2 ruby ruby-on-rails

我有一个模型Leave和一个控制器名称“ LeavesController”。当我将resources :leave[singular]设置为 the 时routes.rb,路线名称就可以了。但是当我设置resources :leaves[plural] 时,我会得到一些有趣的路由名称。有关详细信息,请参阅屏幕截图。[叶,新叶,编辑叶]

我怎样才能摆脱这些有趣的路由名称?

这是我在 routes.rb 中设置 <code>resources :leaves</code>

Mar*_*itt 5

config/initializers/inflections.rb取消注释/添加以下内容

ActiveSupport::Inflector.inflections(:en) do |inflect|
  inflect.irregular 'leave', 'leaves'
end
Run Code Online (Sandbox Code Playgroud)

解释:

Rails 使用 Inflectors 自动将单词单数化/复数化。这就是诸如单数模型名称之类的东西如何转换为其复数表名称的方式。Rails 通常能够猜测正确的复数/单数化,但有时会感到困惑。在inflections.rb初始化,您可以明确地定义你想怎么处理的单一化/复数时,Rails不给你你在找什么。

  • @user5756014 规则定义为 [Here](https://github.com/rails/rails/blob/master/activesupport/lib/active_support/inflections.rb#L41) 它表明如果单词以 `ves` 结尾用 `fe` 替换 `ves` (2认同)