# Reverse of h()

**URL:** <https://www.ruby-forum.com/t/reverse-of-h/94745>\
**Category:** Rails\
**Created:** [November 13, 2005, 2:34am UTC](https://www.ruby-forum.com/t/reverse-of-h/94745 "2005-11-13T02:34:44Z")\
**Posts on this page:** 19\
**Page:** 1

<div class="post-metadata">

**Author:** ![ronnylist](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/ee7513/32.png) [@ronnylist](https://www.ruby-forum.com/u/ronnylist)\
**Post date:** [November 13, 2005, 2:34am UTC](https://www.ruby-forum.com/t/reverse-of-h/94745/1 "2005-11-13T02:34:44Z")

</div>

Hi all,

Is there a function/method that does the reverse of h() ?

I need to decode some html-entities-encoded string back to the actual  
characters, e.g. “&” to “&”, and so on.

By the way, I couldn’t find the documentation for h() in the Rails API  
docs, is it not part of Rails? Where should I look for its docs? I  
thought maybe I could find the reverse function in the docs.

Thanks in advance.

Cheers,  
Ronny

---

<div class="post-metadata">

**Author:** ![ronnylist](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/ee7513/32.png) [@ronnylist](https://www.ruby-forum.com/u/ronnylist)\
**Post date:** [November 13, 2005, 2:46am UTC](https://www.ruby-forum.com/t/reverse-of-h/94745/2 "2005-11-13T02:46:45Z")

</div>

h() is just an alias for html\_escape, but that doesn’t seem to be  
showing up in the docs.

Unfortunately I can’t tell you how to reverse h()…but it seems like  
you might be using it wrong (or I’m using it wrong, so I’d like to be  
corrected!). I was under the impression that it’s just used for  
sanitizing output before it’s sent to the browser, so it would be done  
in a controller or view. It sounds to me like you’re calling h() when  
you insert something into the database, which is unnecessary and  
you’ll end up running into the problems you have.

That may not be your particular situation, but it’s a comment I have  
that may help other people avoid misusing h() in this manner.

Pat

---

<div class="post-metadata">

**Author:** ![ronnylist](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/ee7513/32.png) [@ronnylist](https://www.ruby-forum.com/u/ronnylist)\
**Post date:** [November 13, 2005, 2:52am UTC](https://www.ruby-forum.com/t/reverse-of-h/94745/3 "2005-11-13T02:52:45Z")

</div>

Total guess here, but would CGI::unescapeHTML do what you want?

---

<div class="post-metadata">

**Author:** ![ronnylist](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/ee7513/32.png) [@ronnylist](https://www.ruby-forum.com/u/ronnylist)\
**Post date:** [November 13, 2005, 2:58am UTC](https://www.ruby-forum.com/t/reverse-of-h/94745/4 "2005-11-13T02:58:47Z")

</div>

On Nov 12, 2005, at 8:44 PM, Pat M. wrote:

> h() is just an alias for html\_escape, but that doesn’t seem to be  
> showing up in the docs.

These are in erb and thus don’t show up in the Rails API docs.

---

<div class="post-metadata">

**Author:** ![ronnylist](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/ee7513/32.png) [@ronnylist](https://www.ruby-forum.com/u/ronnylist)\
**Post date:** [November 13, 2005, 6:07am UTC](https://www.ruby-forum.com/t/reverse-of-h/94745/5 "2005-11-13T06:07:59Z")

</div>

On Sat, Nov 12, 2005 at 06:44:43PM -0700, Pat M. wrote:

> h() is just an alias for html\_escape, but that doesn’t seem to be  
> showing up in the docs.

Ah, I see.

> Unfortunately I can’t tell you how to reverse h()…but it seems like  
> you might be using it wrong (or I’m using it wrong, so I’d like to be  
> corrected!). I was under the impression that it’s just used for  
> sanitizing output before it’s sent to the browser, so it would be done  
> in a controller or view.

You’re right. AFAIK, h() is only relevant in the view. I wouldn’t use  
h() in a model or controller.

> It sounds to me like you’re calling h() when you insert something  
> into the database, which is unnecessary and you’ll end up running  
> into the problems you have.

No, my situation is different actually, and it’s a bit complicated (at  
least for me). I want to do an inline text editing with AJAX (a la  
Flickr). Maybe I’m not doing it right, or just complicating things. So  
my asking this is just out of curiosity really.

Anyway, I looked in the source code for html\_encode() and it’s just a  
one-liner regex substitution, so I can create a helper function that  
does the reverse if I want to.

[http://www.ruby-doc.org/stdlib/libdoc/erb/rdoc/classes/ERB/Util.html#M000688](http://www.ruby-doc.org/stdlib/libdoc/erb/rdoc/classes/ERB/Util.html#M000688)

Thanks to everyone who responded. You’ve been very helpful.

Ronny

---

<div class="post-metadata">

**Author:** ![ronnylist](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/ee7513/32.png) [@ronnylist](https://www.ruby-forum.com/u/ronnylist)\
**Post date:** [November 13, 2005, 6:47am UTC](https://www.ruby-forum.com/t/reverse-of-h/94745/6 "2005-11-13T06:47:04Z")

</div>

On Sun, Nov 13, 2005 at 04:04:53PM +1100, Ronny H. wrote:

> Anyway, I looked in the source code for html\_encode() and it’s just a  
> one-liner regex substitution, so I can create a helper function that  
> does the reverse if I want to.
> 
> [http://www.ruby-doc.org/stdlib/libdoc/erb/rdoc/classes/ERB/Util.html#M000688](http://www.ruby-doc.org/stdlib/libdoc/erb/rdoc/classes/ERB/Util.html#M000688)

There’s also a Javascript version included with Rails ([script.aculo.us](http://script.aculo.us)  
actually) if anyone’s interested.

String.prototype.escapeHTML()  
String.prototype.unescapeHTML()

[http://wiki.script.aculo.us/scriptaculous/show/Prototype](http://wiki.script.aculo.us/scriptaculous/show/Prototype)

Ronny

---

<div class="post-metadata">

**Author:** ![ronnylist](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/ee7513/32.png) [@ronnylist](https://www.ruby-forum.com/u/ronnylist)\
**Post date:** [November 13, 2005, 6:50am UTC](https://www.ruby-forum.com/t/reverse-of-h/94745/7 "2005-11-13T06:50:05Z")

</div>

Why aren’t you using the built-in [script.aculo.us](http://script.aculo.us) support that makes  
inline  
text editing almost trivial to implement?

Check out the fairly well hidden documentation for the method  
“in\_place\_editor” on [http://rails.rubyonrails.com/](http://rails.rubyonrails.com/)

---

<div class="post-metadata">

**Author:** ![ronnylist](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/ee7513/32.png) [@ronnylist](https://www.ruby-forum.com/u/ronnylist)\
**Post date:** [November 13, 2005, 6:07am UTC](https://www.ruby-forum.com/t/reverse-of-h/94745/8 "2005-11-13T06:07:59Z")

</div>

On Sat, Nov 12, 2005 at 08:49:49PM -0500, Andrew S. wrote:

> Total guess here, but would CGI::unescapeHTML do what you want?

Yes! That would work too. Even more complete than h()/html\_escape()  
reversed, actually.

Thanks.

Ronny

---

<div class="post-metadata">

**Author:** ![ronnylist](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/ee7513/32.png) [@ronnylist](https://www.ruby-forum.com/u/ronnylist)\
**Post date:** [November 13, 2005, 7:23am UTC](https://www.ruby-forum.com/t/reverse-of-h/94745/9 "2005-11-13T07:23:13Z")

</div>

On Sat, Nov 12, 2005 at 09:48:32PM -0800, Tom F. wrote:

> Why aren’t you using the built-in [script.aculo.us](http://script.aculo.us) support that makes  
> inline text editing almost trivial to implement?
> 
> Check out the fairly well hidden documentation for the method  
> “in\_place\_editor” on [http://rails.rubyonrails.com/](http://rails.rubyonrails.com/)

D’oh! _slaps head hard_

I’m so glad I asked the list. I really learned a lot of new things  
from this list. I’ll definitely look into [script.aculo.us](http://script.aculo.us) more.

Thanks, Tom.

Ronny

---

<div class="post-metadata">

**Author:** ![ronnylist](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/ee7513/32.png) [@ronnylist](https://www.ruby-forum.com/u/ronnylist)\
**Post date:** [November 13, 2005, 2:35pm UTC](https://www.ruby-forum.com/t/reverse-of-h/94745/10 "2005-11-13T14:35:36Z")

</div>

There is no documentation for “in\_place\_editor” on  
[http://rails.rubyonrails.com/](http://rails.rubyonrails.com/)  
It’s completely blank.

The exact blank page is  
[http://rails.rubyonrails.com/files/vendor/rails/actionpack/lib/action\_controller/macros/in](http://rails.rubyonrails.com/files/vendor/rails/actionpack/lib/action_controller/macros/in)  
\_place\_editing\_rb.html

Warren S.

---

<div class="post-metadata">

**Author:** ![ronnylist](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/ee7513/32.png) [@ronnylist](https://www.ruby-forum.com/u/ronnylist)\
**Post date:** [November 13, 2005, 2:53pm UTC](https://www.ruby-forum.com/t/reverse-of-h/94745/11 "2005-11-13T14:53:38Z")

</div>

On Sun, Nov 13, 2005 at 03:34:15PM +0200, Warren S. wrote:

> There is no documentation for “in\_place\_editor” on [http://rails.rubyonrails.com/](http://rails.rubyonrails.com/)  
> It’s completely blank.
> 
> The exact blank page is  
> [Peak Obsession](http://rails.rubyonrails.com/files/vendor/rails/actionpack/lib/action_controller/macros/in)  
> \_place\_editing\_rb.html
> 
> Warren S.

I found this page:  
[http://wiki.rubyonrails.com/rails/pages/HowToStyleInPlaceEditorWithCss](http://wiki.rubyonrails.com/rails/pages/HowToStyleInPlaceEditorWithCss)

which brought me here:  
[http://wiki.script.aculo.us/scriptaculous/show/Ajax.InPlaceEditor](http://wiki.script.aculo.us/scriptaculous/show/Ajax.InPlaceEditor)

HTH.

Ronny

---

<div class="post-metadata">

**Author:** ![ronnylist](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/ee7513/32.png) [@ronnylist](https://www.ruby-forum.com/u/ronnylist)\
**Post date:** [November 13, 2005, 5:55pm UTC](https://www.ruby-forum.com/t/reverse-of-h/94745/12 "2005-11-13T17:55:41Z")

</div>

Did anybody succeeded in using InPlaceEditing inside a partial  
collection?  
It’s working perfectly in a regular template, but the id part becomes  
blank when it’s inside a partial collection.

article\_controller.rb:  
class ArticleController \< ApplicationController  
in\_place\_edit\_for :comment, :body  
…  
end

\_comment.rhtml:

- \<%= in\_place\_editor\_field :comment, :body %\>

show.rhtml:  
…

\<% unless @article.comments == nil %\> \<%= render :partial =\> "comment", :collection =\> @article.comments %\> \<% end %\>
....

Generated HTML:  
…

- ....

[comment\_body\_\_in\_place\_editor] should be  
[comment\_body\_21\_in\_place\_editor].  
Apparently, the id is not set properly.

More odd thing is that when a comment is added using AJAX, above code  
is working again.

I also tried to include :id as follows:  
\<%= in\_place\_editor\_field :comment, :body, { :id =\> comment.id } %\>

But in this case, [comment\_body\_\_in\_place\_editor] only becomes [21],  
but not [comment\_body\_21\_in\_place\_editor]

Here are the docs for InPlaceEditing:  
[http://rails.rubyonrails.com/classes/ActionController/Macros/InPlaceEditing/ClassMethods.html](http://rails.rubyonrails.com/classes/ActionController/Macros/InPlaceEditing/ClassMethods.html)  
[http://rails.rubyonrails.com/classes/ActionView/Helpers/JavaScriptMacrosHelper.html#M000367](http://rails.rubyonrails.com/classes/ActionView/Helpers/JavaScriptMacrosHelper.html#M000367)

What am I missing here?

–Joon

---

<div class="post-metadata">

**Author:** ![ronnylist](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/ee7513/32.png) [@ronnylist](https://www.ruby-forum.com/u/ronnylist)\
**Post date:** [November 13, 2005, 5:59pm UTC](https://www.ruby-forum.com/t/reverse-of-h/94745/13 "2005-11-13T17:59:31Z")

</div>

alex wrote:

> Ronny H. wrote:
> 
> > actually) if anyone’s interested.
> > 
> > String.prototype.escapeHTML()  
> > String.prototype.unescapeHTML()
> 
> The site seems to be unreachable for me at the moment, so I can’t check  
> the docs, but how well do those functions cope with different character  
> sets?

These functions won’t touch anything except &"\<\>.

---

<div class="post-metadata">

**Author:** ![ronnylist](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/ee7513/32.png) [@ronnylist](https://www.ruby-forum.com/u/ronnylist)\
**Post date:** [November 13, 2005, 3:38pm UTC](https://www.ruby-forum.com/t/reverse-of-h/94745/14 "2005-11-13T15:38:44Z")

</div>

Ronny H. wrote:

> actually) if anyone’s interested.
> 
> String.prototype.escapeHTML()  
> String.prototype.unescapeHTML()

The site seems to be unreachable for me at the moment, so I can’t check  
the docs, but how well do those functions cope with different character  
sets?

---

<div class="post-metadata">

**Author:** ![ronnylist](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/ee7513/32.png) [@ronnylist](https://www.ruby-forum.com/u/ronnylist)\
**Post date:** [November 13, 2005, 6:07pm UTC](https://www.ruby-forum.com/t/reverse-of-h/94745/15 "2005-11-13T18:07:42Z")

</div>

Andreas S. wrote:

> > the docs, but how well do those functions cope with different character  
> > sets?
> 
> These functions won’t touch anything except &"\<\>.

Excellent. Just what I need.

---

<div class="post-metadata">

**Author:** ![ronnylist](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/ee7513/32.png) [@ronnylist](https://www.ruby-forum.com/u/ronnylist)\
**Post date:** [December 4, 2005, 3:23am UTC](https://www.ruby-forum.com/t/reverse-of-h/94745/16 "2005-12-04T03:23:30Z")

</div>

Speaking of hard to find…

Where is the ‘h’ function defined?

Thanks,

-Kelly

---

<div class="post-metadata">

**Author:** ![ronnylist](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/ee7513/32.png) [@ronnylist](https://www.ruby-forum.com/u/ronnylist)\
**Post date:** [November 14, 2005, 4:45am UTC](https://www.ruby-forum.com/t/reverse-of-h/94745/17 "2005-11-14T04:45:04Z")

</div>

The [rails.rubyonrails.com](http://rails.rubyonrails.com) site is a frames based one, when I drill down  
to  
the docs, the frame URL I get to is this one:

[http://rails.rubyonrails.com/classes/ActionView/Helpers/JavaScriptMacrosHelp](http://rails.rubyonrails.com/classes/ActionView/Helpers/JavaScriptMacrosHelp)  
er.html#M000366

I did say it was hard to find 🙂

---

<div class="post-metadata">

**Author:** ![ronnylist](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/ee7513/32.png) [@ronnylist](https://www.ruby-forum.com/u/ronnylist)\
**Post date:** [December 17, 2005, 2:35am UTC](https://www.ruby-forum.com/t/reverse-of-h/94745/18 "2005-12-17T02:35:43Z")

</div>

railsinator wrote:

> Speaking of hard to find…
> 
> Where is the ‘h’ function defined?
> 
> Thanks,
> 
> -Kelly

it’s an alias of humanize…

look for that 🙂

Jake

---

<div class="post-metadata">

**Author:** ![ronnylist](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/ee7513/32.png) [@ronnylist](https://www.ruby-forum.com/u/ronnylist)\
**Post date:** [December 17, 2005, 7:23am UTC](https://www.ruby-forum.com/t/reverse-of-h/94745/19 "2005-12-17T07:23:38Z")

</div>

Jacob Stetser wrote:

> > 
> 
> it’s an alias of humanize…
> 
> look for that 🙂
> 
> Jake

No. It’s an alias for html\_escape. See  
[http://www.ruby-doc.org/stdlib/libdoc/erb/rdoc/classes/ERB/Util.html](http://www.ruby-doc.org/stdlib/libdoc/erb/rdoc/classes/ERB/Util.html)

– stefan
