我正在开发 Rails 4.1.0。我已经在我的应用程序中生成了 leaves_controller 和 Leave 模型。
但是应用程序为叶子生成了路由,例如 new_leaf、edit_leaf 等。
实际上我只想要Leave的单数字符串为Leave,如new_leave_path、edit_leave_path。
如果有任何在 Rails 中将类名单数化的想法,请分享。
最佳答案
如果您只想更改路线,您可以使用 as:
选项:
#config/routes.rb
resources :leaves, as: "leave"
--
或者,如果您想在 Rails 中设置术语,您可能希望使用 Inflector
,如下所示:
How do I override rails naming conventions?
#config/initializers/inflectors.rb
# Add new inflection rules using the following format
ActiveSupport::Inflector.inflections do |inflect|
inflect.irregular 'leave', 'leaves'
end
关于ruby-on-rails - 如何自定义rails中类名的单数形式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24177851/