我正在尝试编写一个助手来翻译来自 "something_like_this"
的字符串至 "Something like this"
.我正在使用 "something_like_this".titlecase
将其发送至 "Something Like This"
但除了第一个之外,我一直把每个大写字母都小写。
我想我正在寻找这样的东西:
def write_sentence
string.titlecase.gsub!(/UPPERCASE-TO-LOWERCASE-EXCEPT-FIRST/)
#that should be something to lowercase everything except the first letter
return string
end
所以在 View 中我可以写
string.write_sentence
并让它完全返回我想要的。有什么想法吗?谢谢!
编辑
我应该提到字符串有时可能只是一个单词,在这种情况下,字符串应该从
"something"
转换而来。至 "Something"
.
最佳答案
尝试这个,
"something_like_this".humanize
http://api.rubyonrails.org/classes/ActiveSupport/Inflector.html#method-i-humanize
关于ruby-on-rails - 从标题案例到句子案例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15605320/