Rails文章助手-"a"或“一个" [英] Rails article helper - "a" or "an"

查看:62
本文介绍了Rails文章助手-"a"或“一个"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人知道可以自动将适当的文章添加到给定字符串的Rails Helper吗?例如,如果我将苹果"传递给函数,它将变成一个苹果",而如果我发送香蕉",它将返回香蕉"

Does anyone know of a Rails Helper which can automatically prepend the appropriate article to a given string? For instance, if I pass in "apple" to the function it would turn out "an apple", whereas if I were to send in "banana" it would return "a banana"

我已经检查了Rails TextHelper模块,但找不到任何内容.抱歉,如果这是重复项,但是对于搜索...来说,这无疑是一个艰难的答案.

I already checked the Rails TextHelper module but could not find anything. Apologies if this is a duplicate but it is admittedly a hard answer to search for...

推荐答案

我不知道,但是为这个权利写一个助手似乎很简单? 不在我的头上

None that I know of but it seems simple enough to write a helper for this right? Off the top of my head

def indefinite_articlerize(params_word)
    %w(a e i o u).include?(params_word[0].downcase) ? "an #{params_word}" : "a #{params_word}"
end

希望有帮助

edit 1:还发现此线程带有修补程序,可以帮助您进一步防弹

edit 1: Also found this thread with a patch that might help you bulletproof this more https://rails.lighthouseapp.com/projects/8994/tickets/2566-add-aan-inflector-indefinitize

这篇关于Rails文章助手-"a"或“一个"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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