如何自定义rails中类名的单数化

Pra*_*abu 2 ruby-on-rails rails-routing

我正在研究 Rails 4.1.0。我在我的应用程序中生成了leaves_controller 和 Leave 模型。

但是应用程序为叶子生成了路由,例如 new_leafe、edit_leaf 等。

实际上,我只希望将 Leave 的单数化字符串保留为 Leave,例如 new_leave_path、edit_leave_path。

如果有任何想法在 Rails 中将类名单一化,请分享。

Ric*_*eck 5

如果只是您希望更改的路线,则可以使用以下as:选项:

#config/routes.rb
resources :leaves, as: "leave"
Run Code Online (Sandbox Code Playgroud)

——

或者,如果您想在 Rails 中设置术语,您可能希望使用Inflector这样的:

如何覆盖 rails 命名约定?

#config/initializers/inflectors.rb
# Add new inflection rules using the following format 
ActiveSupport::Inflector.inflections do |inflect|
  inflect.irregular 'leave', 'leaves'
end
Run Code Online (Sandbox Code Playgroud)