Reverse of h()


#1

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


#2

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


#3

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


#4

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.


#5

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

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

Ronny


#6

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

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

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

http://wiki.script.aculo.us/scriptaculous/show/Prototype

Ronny


#7

Why aren’t you using the built-in 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/


#8

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


#9

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

Why aren’t you using the built-in 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/

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 more.

Thanks, Tom.

Ronny


#10

There is no documentation for “in_place_editor” on
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
_place_editing_rb.html

Warren S.


#11

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/
It’s completely blank.

The exact blank page is
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

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

HTH.

Ronny


#12

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/ActionView/Helpers/JavaScriptMacrosHelper.html#M000367

    What am I missing here?

    –Joon


  • #13

    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 &"<>.


    #14

    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?


    #15

    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.


    #16

    Speaking of hard to find…

    Where is the ‘h’ function defined?

    Thanks,

    -Kelly


    #17

    The 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
    er.html#M000366

    I did say it was hard to find :slight_smile:


    #18

    railsinator wrote:

    Speaking of hard to find…

    Where is the ‘h’ function defined?

    Thanks,

    -Kelly

    it’s an alias of humanize…

    look for that :slight_smile:

    Jake


    #19

    Jacob Stetser wrote:

    it’s an alias of humanize…

    look for that :slight_smile:

    Jake

    No. It’s an alias for html_escape. See
    http://www.ruby-doc.org/stdlib/libdoc/erb/rdoc/classes/ERB/Util.html

    – stefan