undefined方法复数为main:Object

Min*_*ohn 13 ruby ruby-on-rails pluralize

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

pluralize(1, 'person')
Run Code Online (Sandbox Code Playgroud)

不会工作..

输出:

NoMethodError: undefined method 'pluralize' for main:Object
from (pry):42:in '<main>'
Run Code Online (Sandbox Code Playgroud)

helper.method(:pluralize)告诉我:Method: ActionView::Base(ActionView::Helpers::TextHelper)#pluralize

我错过了什么?

Dyl*_*kow 26

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

>> include ActionView::Helpers::TextHelper
>> pluralize(1, 'person')
# => "1 person"
Run Code Online (Sandbox Code Playgroud)

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

>> helper.pluralize(1, 'person')
# => "1 person"
Run Code Online (Sandbox Code Playgroud)