有没有帮助避免这种代码?
= @leads.length == 1 ? 'is' : 'are'
Run Code Online (Sandbox Code Playgroud)
我知道复数,但在这种情况下没有帮助.我知道写一个帮助器是微不足道的,我想知道Rails API中是否存在我忽略的内容.
谢谢,
-Tim
sco*_*ttd 18
正如t6d所说,你需要在Rails 变换器中添加is/are.只是来自td6的代码,但您需要添加ActiveSupport名称空间:
ActiveSupport::Inflector.inflections do |inflection|
inflection.irregular "is", "are"
end
Run Code Online (Sandbox Code Playgroud)
现在,内置的辅助Rails是复数,但在视图中不会做你想要的.例如:
pluralize(@leads.length,'is')
Run Code Online (Sandbox Code Playgroud)
输出(2个引线)
2 are
Run Code Online (Sandbox Code Playgroud)
对于你想要的东西,你需要制作自己的助手,使单词多元化,但不输出计数.所以如果你把它基于当前的轨道复数:
def pluralize_no_count(count, singular, plural = nil)
((count == 1 || count == '1') ? singular : (plural || singular.pluralize))
end
Run Code Online (Sandbox Code Playgroud)
也许你可以使用复数并为“is”添加变形
Inflector.inflections do |inflection|
inflection.irregular "is", "are"
end
Run Code Online (Sandbox Code Playgroud)
以便
'is'.pluralize # => 'are'
Run Code Online (Sandbox Code Playgroud)
(我没有测试该解决方案,但它应该有效。)
| 归档时间: |
|
| 查看次数: |
3981 次 |
| 最近记录: |