在rails控制台中使用html_escape等辅助方法

hig*_*dth 10 ruby-on-rails html-escape

我试图在我的视图中看到我的变量编码出了什么问题.所以我启动了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
Run Code Online (Sandbox Code Playgroud)

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

bou*_*der 10

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

include ERB::Util
Run Code Online (Sandbox Code Playgroud)

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


jvn*_*ill 9

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

helper.send(:html_escape, '123')
helper.pluralize 3, 'user'
Run Code Online (Sandbox Code Playgroud)