Forum: Ruby on Rails What is the h for?

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.
3572030efe923a81eea7e6d3649792c2?d=identicon&s=25 Harro (Guest)
on 2006-04-08 14:41
In the "Agile Web Development with Rails" book it runs through making an
example application, a shopping cart.

In one of the views files, there is one line of code I dont understand
(below), I dont know what the "h" is for, also I took the h out, and it
did not affect the application (as far as i could see).

<%= h(product.title) %>

Can anyone shed some light on this for me please.

Thanks very much, Harro
7223c62b7310e164eb79c740188abbda?d=identicon&s=25 Xavier Noria (Guest)
on 2006-04-08 14:52
(Received via mailing list)
On Apr 8, 2006, at 14:41, Harro wrote:

>
> Can anyone shed some light on this for me please.

It escapes special HTML characters. If the title is "Bonnie & Clyde"
you get the valid "Bonnie &amp; Clyde" thanks to h(). You want to use
h() practically always.

-- fxn
22ff4abebc9bfd299524bb14449d95dc?d=identicon&s=25 Erik van Oosten (Guest)
on 2006-04-08 14:55
(Received via mailing list)
It escapes the input for display in HTML.

irb(main):001:0> require 'erb'
=> true
irb(main):002:0> include ERB::Util
=> Object
irb(main):003:0> h('<b>Hi There</b>')
=> "&lt;b&gt;Hi There&lt;/b&gt;"
irb(main):004:0>

     Erik.


Harro schreef:
Ec5a599777854c540fd102ef4691fe10?d=identicon&s=25 Rimantas Liubertas (Guest)
on 2006-04-08 14:58
(Received via mailing list)
> It escapes the input for display in HTML.
<...>
> > In the "Agile Web Development with Rails" book it runs through making an
> > example application, a shopping cart.
<...>

And you can read about it in the same book, pages 331-332 if I am not
mistaken.


Regards,
Rimantas
--
http://rimantas.com/
3572030efe923a81eea7e6d3649792c2?d=identicon&s=25 Harro (Guest)
on 2006-04-08 15:13
Rimantas Liubertas wrote:
>
> And you can read about it in the same book, pages 331-332 if I am not
> mistaken.
>
>
> Regards,
> Rimantas

Thanks all for the help. I really should hav read on :S.

Thanks again, Harro.
963e68decf3aa66744de537db2c0ea0a?d=identicon&s=25 Lieutenant Ro (lieutenant-ro)
on 2006-04-10 16:16
The actual pages on h() or html_escape() from Agile Web Development are:
344 and 443. Just FYI.

--
ro

Rimantas Liubertas wrote:
>> It escapes the input for display in HTML.
> <...>
>> > In the "Agile Web Development with Rails" book it runs through making an
>> > example application, a shopping cart.
> <...>
>
> And you can read about it in the same book, pages 331-332 if I am not
> mistaken.
>
>
> Regards,
> Rimantas
> --
> http://rimantas.com/
This topic is locked and can not be replied to.