【问题标题】:Using helper methods like html_escape in rails console在 Rails 控制台中使用 html_escape 等辅助方法
【发布时间】:2013-03-01 17:29:45
【问题描述】:

我想看看在我看来我的变量编码出了什么问题。所以我启动了rails控制台并尝试做

$ rails console
Loading development environment (Rails 3.2.11)
irb(main):001:0> html_escape({:a=>1, :b=>"my str"})
NoMethodError: undefined method `html_escape' for main:Object

如何在 Rails 控制台中使用 h 或 html_escape?

【问题讨论】:

    标签: ruby-on-rails html-escape


    【解决方案1】:

    很容易解决。 html_escape 在 ERB::Util 中定义,所以简单地写:

    include ERB::Util
    

    在第一次使用 html_escape 之前在您的控制台中

    【讨论】:

    • 知道 html_escape 辅助方法的作用吗? (菜鸟试图学习。)
    • 它使用提供的字符串调用 CGI.escapeHTML
    【解决方案2】:

    您可以通过helper 调用它。有些方法是私有的,所以你可能需要使用 send 来调用它们

    helper.send(:html_escape, '123')
    helper.pluralize 3, 'user'
    

    【讨论】:

    • 看来我今天收到了一些。
    【解决方案3】:
    > helper.send(:html_escape, '{ a: 1, b: "my str" }')
    "{ a: 1, b: "my str" }"
    

    【讨论】:

    • 你也应该检查答案,如果已经回答了,那么你不需要再次写相同的答案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-09-14
    • 2012-02-13
    • 1970-01-01
    • 1970-01-01
    • 2014-12-08
    • 2011-05-05
    相关资源
    最近更新 更多