为什么我不能在 rails 控制台中使用复数方法?

Soo*_*e J 2 ruby ruby-on-rails

我刚刚才知道我不能在 rails 控制台或 IRB 中使用“复数”方法。这有什么我不明白的吗?

2.3.0 :001 > pluralize
NameError: undefined local variable or method `pluralize' for main:Object
Run Code Online (Sandbox Code Playgroud)

当它在 ruby​​ 或视图文件中使用时,它会得到很好的解释。为什么我不能在 rails 控制台中使用它?

R. *_*rra 5

pluralizeRails 视图中使用的方法在ActionView::Helpers::TextHelper. 要在 rails 控制台中使用它,您需要包含它

$ rails console
2.3.3 :008 > include ActionView::Helpers::TextHelper
2.3.3 :009 > pluralize 2, 'man'
=> "2 men"
Run Code Online (Sandbox Code Playgroud)

或通过辅助变量调用它们

$ rails console
2.3.3 :0010 > helper.pluralize(2, 'man')
=> "2 men"
Run Code Online (Sandbox Code Playgroud)