【问题标题】:undefined method pluralize for main:Objectmain:Object 的未定义方法复数
【发布时间】:2014-09-23 13:43:02
【问题描述】:

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

pluralize(1, 'person')

不会工作..

输出:

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

helper.method(:pluralize) 向我展示了:Method: ActionView::Base(ActionView::Helpers::TextHelper)#pluralize

我错过了什么?

【问题讨论】:

    标签: ruby-on-rails ruby pluralize


    【解决方案1】:

    默认情况下,助手不包含在控制台中。您可以先包含它们,它会起作用:

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

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

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

    【讨论】:

    • 我在做include TextHelper.. 不知道我必须包括整个事情。谢谢迪伦。
    • 这破坏了 MVC,但我一直这样做:-0
    猜你喜欢
    • 2012-05-05
    • 1970-01-01
    • 2015-01-23
    • 2023-03-11
    • 1970-01-01
    • 2012-04-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多