问题描述
我正在用首字母缩略词定义一些活动记录.RvPark(休闲车公园).当我为班级名称命名时,我得到了Rv Park".它真的应该是房车公园".有没有好的方法可以做到这一点?由于这个模型与其他模型共享代码,我需要创建一个通用的解决方案,但我一直无法想出一个.
I am defining some active records with acronyms. RvPark (Recreational Vehicle Park). When I titleize the class name, I get 'Rv Park'. It really should be 'RV Park'. Is there a good way to do this? Since this model shares code with other models, I need to create a generic solution, but I haven't been able to come up with one.
我确实看到了有关此问题的讨论,但没有对我有用的解决方案.任何见解表示赞赏.
I did see a discussion on this, but there wasn't a solution that worked for me. any insight is appreciated.
推荐答案
UPDATE: Acronym support was发布后添加到 Rails Inflector.
UPDATE: Acronym support was added to the Rails Inflector after I posted this.
请参阅 @Anson 的回答,了解 Rails 3.2 及更高版本.
See @Anson's answer for Rails 3.2 and up.
这看起来像是 titleize
没有设计的边缘情况.问题是里面的 capitalize
调用总是会把 RV 变成 Rv.
This looks like an edge-case that titleize
wasn't designed for. The problem is the capitalize
call inside will always turn RV into Rv.
我会为在内部调用 self.class.titleize
的模型创建一个通用名称函数,然后在 RVPark 模型中重载它.
I would create a generic name function for the models that call self.class.titleize
internally, and then overload it in the RVPark model.
这篇关于在 Rails 中使用 titleize 作为首字母缩略词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!