如何覆盖 Rails 命名约定? [英] How do I override rails naming conventions?

查看:30
本文介绍了如何覆盖 Rails 命名约定?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为clothing"的模型,我想将其作为单数(一件衣服).默认情况下,rails 说复数是服装.对还是错,我觉得如果复数是clothes"会更易读.

I have a model named "clothing" which I want to be the singlular (one piece of clothing). By default, rails says the plural is clothings. Right or wrong, I think it will be more readable if the plural is "clothes".

如何覆盖复数命名约定?我可以在模型中正确地做,这样我就不必一遍又一遍地做吗?这将如何改变路由的处理方式(我使用的是 Restful 架构)?

How do I override the plural naming convention? Can I do it right in the model so I don't have to do it over and over? How will this change how routes are handled (I am using restful architecture)?

推荐答案

我不是 RoR 专家,但确实找到了 可能的方法.从引用的站点,您可以在 config/initializers/inflections.rb 文件中添加变形规则:

I'm no RoR expert, but did find a possible approach. From the referenced site you can add inflection rule inside the config/initializers/inflections.rb file:

# Add new inflection rules using the following format 
ActiveSupport::Inflector.inflections do |inflect|
  inflect.irregular 'clothing', 'clothes'
end

这篇关于如何覆盖 Rails 命名约定?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆