undefined方法复数为main:Object

时间:2022-03-17 23:54:53

I'm trying to test a method in my console, but even the basic pluralize -

我正在尝试在我的控制台中测试一个方法,但即使是基本的复数 -

pluralize(1, 'person')

wont work..

不会工作..

Output:

输出:

NoMethodError: undefined method 'pluralize' for main:Object
from (pry):42:in '<main>'

but helper.method(:pluralize) shows me : Method: ActionView::Base(ActionView::Helpers::TextHelper)#pluralize

但是helper.method(:pluralize)显示了我:方法:ActionView :: Base(ActionView :: Helpers :: TextHelper)#pluralize

What am i missing?

我错过了什么?

1 个解决方案

#1


25  

The helpers aren't included by default in the console. You can include them first and it'll work:

默认情况下,控制台中不包含帮助程序。你可以先包含它们,它会起作用:

>> include ActionView::Helpers::TextHelper
>> pluralize(1, 'person')
# => "1 person"

Or, you can use the helper object which Rails gives you in the console:

或者,您可以使用Rails在控制台中为您提供的帮助程序对象:

>> helper.pluralize(1, 'person')
# => "1 person"

#1


25  

The helpers aren't included by default in the console. You can include them first and it'll work:

默认情况下,控制台中不包含帮助程序。你可以先包含它们,它会起作用:

>> include ActionView::Helpers::TextHelper
>> pluralize(1, 'person')
# => "1 person"

Or, you can use the helper object which Rails gives you in the console:

或者,您可以使用Rails在控制台中为您提供的帮助程序对象:

>> helper.pluralize(1, 'person')
# => "1 person"