2013-03-01 54 views
8

我想看看我的視圖中的變量編碼出了什麼問題。所以我啓動了滑軌控制檯並試圖做在rails控制檯中使用像html_escape這樣的幫助器方法

$ 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?

回答

8

你可以稱之爲helper。有些方法是私人的,所以你可能需要用send來打電話給他們

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

downvoter的前控制檯,請告訴我們,什麼是錯的,所以我們可以學習。 – highBandWidth 2013-03-01 22:58:40

+0

看起來像今天我得到了一些。 – jvnill 2013-03-02 00:21:50

+0

非常有用的提示! – jmoreira 2014-09-09 13:06:15

6

容易解決。 html_escape在ERB ::的Util定義,因此簡單地寫:

include ERB::Util 

在您第一次使用html_escape

+0

任何想法html_escape輔助方法做什麼? (noob試圖學習。) – BKSpurgeon 2015-09-14 21:30:46

+0

它使用提供的字符串 – SunnyMagadan 2016-05-31 13:14:05

0
> helper.send(:html_escape, '{ a: 1, b: "my str" }') 
"{ a: 1, b: "my str" }" 
+2

調用CGI.escapeHTML,你應該檢查答案,如果已經有答案,那麼你不需要再次寫相同的答案。 – 2013-03-01 17:59:55

相關問題