Using helper methods like html_escape in rails console
Asked Answered
C

3

10

I am trying to see what is going wrong with my encoding of variables in my view. So I fire up rails console and try to do

$ 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

How do I use h or html_escape in rails console?

Capparidaceous answered 1/3, 2013 at 17:29 Comment(0)
H
10

you call it through helper. some methods are private so you may need to use send to call them

helper.send(:html_escape, '123')
helper.pluralize 3, 'user'
Helli answered 1/3, 2013 at 17:32 Comment(1)
looks like I getting a few of those today.Helli
K
12

Easy to solve. html_escape is defined in ERB::Util so simply write:

include ERB::Util

in your console prior to the first use of html_escape

Kuopio answered 1/3, 2013 at 17:33 Comment(2)
any idea what the html_escape helper method does? (noob trying to learn.)Strobila
It calls CGI.escapeHTML with provided stringFormalize
H
10

you call it through helper. some methods are private so you may need to use send to call them

helper.send(:html_escape, '123')
helper.pluralize 3, 'user'
Helli answered 1/3, 2013 at 17:32 Comment(1)
looks like I getting a few of those today.Helli
N
0
> helper.send(:html_escape, '{ a: 1, b: "my str" }')
"{ a: 1, b: "my str" }"
Notary answered 1/3, 2013 at 17:56 Comment(1)
you should check the answers as well, if something is already answered, then you do not need to write the same answer again.Ainu

© 2022 - 2024 — McMap. All rights reserved.