Forum: Ruby on Rails Special characters in ERB

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.
Jennyw J. (Guest)
on 2006-03-15 04:59
(Received via mailing list)
I'm pulling text out of a database that contains special characters,
like the trademark sign. For example, a typical string might be like
this:

string_from_database => "Some Special Brand\231 is for sale this
Thursday through Friday."

The \231 is the trademark sign (TM). ERB converts this to question
marks.  So, my question is -- how can I get it to display the trademark
sign? They have another Web site using PHP which just sends what's in
the database out to the Web without substitutions. This would work
better than the substitution to ? signs, but it'd be even better to
convert it to the appropriate HTML entity.

Does anyone know of a plug-in or anything that will do this for the
trademark symbol and other characters? Or a way to bypass ERB's
substitution?

Thanks!

Jen
Ovidiu EFTIMIE (Guest)
on 2006-03-15 12:42
(Received via mailing list)
try this :
string_from_database => 'Some Special Brand\231 is for sale this
Thursday through Friday.'

single quotes
Alex Y. (Guest)
on 2006-03-15 13:02
(Received via mailing list)
jennyw wrote:
> better than the substitution to ? signs, but it'd be even better to
> convert it to the appropriate HTML entity.
>
> Does anyone know of a plug-in or anything that will do this for the
> trademark symbol and other characters? Or a way to bypass ERB's
> substitution?
Sounds to me like you've got your character sets in a twist.  \231
certainly isn't  a trademark sign in UTF-8, and question-mark
substitution is what browsers do when they come across a character they
don't understand in the current context.  I don't believe ERb does any
substitution like that at all.  Try explicitly setting the character set
of your page to ISO-8859-1 (or CP-1252, for that matter...  I think
they're the same in that range) in the HTML <head> and in the HTTP
header, and see how it comes out.
Jennyw J. (Guest)
on 2006-03-15 23:04
(Received via mailing list)
Alex Y. wrote:
> Sounds to me like you've got your character sets in a twist.  \231
> certainly isn't  a trademark sign in UTF-8, and question-mark
> substitution is what browsers do when they come across a character
> they don't understand in the current context.  I don't believe ERb
> does any substitution like that at all.  Try explicitly setting the
> character set of your page to ISO-8859-1 (or CP-1252, for that
> matter...  I think they're the same in that range) in the HTML <head>
> and in the HTTP header, and see how it comes out.
Thanks, Alex. This was it!

FYI, at first I tried:

 >    headers["Content-Type"] = "text/html"

which thew me for a loop at first, since even though I don't specify a
charset above it shows up as UTF-8 (I tried it that way because that's
what the original PHP Web site did). Then I realized that Rails (or
something) adds charset after "text/html" whether you set it or not.
Adding "; charset=ISO-8859-1" worked just fine.

Jen
This topic is locked and can not be replied to.