Forum: Ruby on Rails reverse of h()

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
ronnylist (Guest)
on 2005-11-13 02:34
(Received via mailing list)
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
pergesu (Guest)
on 2005-11-13 02:46
(Received via mailing list)
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
stonelists (Guest)
on 2005-11-13 02:52
(Received via mailing list)
Total guess here, but would CGI::unescapeHTML do what you want?
scott (Guest)
on 2005-11-13 02:58
(Received via mailing list)
On Nov 12, 2005, at 8:44 PM, Pat Maddox 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.
ronnylist (Guest)
on 2005-11-13 06:07
(Received via mailing list)
On Sat, Nov 12, 2005 at 06:44:43PM -0700, Pat Maddox 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/cla...

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

Ronny
ronnylist (Guest)
on 2005-11-13 06:07
(Received via mailing list)
On Sat, Nov 12, 2005 at 08:49:49PM -0500, Andrew Stone 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
ronnylist (Guest)
on 2005-11-13 06:47
(Received via mailing list)
On Sun, Nov 13, 2005 at 04:04:53PM +1100, Ronny Haryanto 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/cla...

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
tom (Guest)
on 2005-11-13 06:50
(Received via mailing list)
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/
ronnylist (Guest)
on 2005-11-13 07:23
(Received via mailing list)
On Sat, Nov 12, 2005 at 09:48:32PM -0800, Tom Fakes 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
warrens (Guest)
on 2005-11-13 14:35
(Received via mailing list)
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/ac...
_place_editing_rb.html

Warren Seltzer
ronnylist (Guest)
on 2005-11-13 14:53
(Received via mailing list)
On Sun, Nov 13, 2005 at 03:34:15PM +0200, Warren Seltzer 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/ac...
> _place_editing_rb.html
>
> Warren Seltzer

I found this page:
http://wiki.rubyonrails.com/rails/pages/HowToStyle...

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

HTH.

Ronny
alex (Guest)
on 2005-11-13 15:38
(Received via mailing list)
Ronny Haryanto 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?
24fc43f079a3de766a1e578750988bad?d=identicon&s=25 Joon L. (seouri)
on 2005-11-13 17:55
(Received via mailing list)
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:
<li id="comment-<%= comment.id %>">
<%= in_place_editor_field :comment, :body %>
</li>

show.rhtml:
....
<ol id="comments">
<% unless @article.comments == nil %>
<%= render :partial => "comment", :collection => @article.comments %>
<% end %>
</ol>
....

Generated HTML:
....
<li id="comment-21">
<span class="in_place_editor_field" id="comment_body__in_place_editor"
tag="span"></span><script type="text/javascript">
//<![CDATA[
new Ajax.InPlaceEditor('comment_body__in_place_editor',
'/article/set_comment_body')
//]]>
</script>
....

[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/ActionControl...
http://rails.rubyonrails.com/classes/ActionView/He...

What am I missing here?

--Joon
40d29f4725ff04ff0d69ccea0bcbdc11?d=identicon&s=25 Andreas S. (andreas)
on 2005-11-13 17:59
alex wrote:
> Ronny Haryanto 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 &\"<>.
alex (Guest)
on 2005-11-13 18:07
(Received via mailing list)
Andreas Schwarz 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.
tom (Guest)
on 2005-11-14 04:45
(Received via mailing list)
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/He...
er.html#M000366

I did say it was hard to find :-)
railsinator (Guest)
on 2005-12-04 03:23
(Received via mailing list)
Speaking of hard to find...

Where is the 'h' function defined?

Thanks,

-Kelly
Jacob Stetser (Guest)
on 2005-12-17 02:35
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
Stefan Kaes (Guest)
on 2005-12-17 07:23
(Received via mailing list)
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/cla...

-- stefan
This topic is locked and can not be replied to.