Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to use ASCII chracter codes in rails link helpers?

This:

<%= link_to "My Trademark &153;", trade_mark_path %>

Generates

"My Trademark &153;"

When I need it to actually be: My Trademark™

Is there anyway to use ASCII codes in the URL helper? I can always resort to generating the link manually, but it's real ugly.

like image 682
Slick23 Avatar asked Nov 23 '11 18:11

Slick23


2 Answers

As Phrogz said, the ideal way forward is just to include the raw character in source code, ensuring it's saved as the right encoding.

If you can't do that due to limitations of your platform or text editor, use the string literal escape \u, not HTML-encoding, to get the character you want directly:

<%= link_to "My Trademark\u2122", trade_mark_path %>

The trademark character being U+2122, or in decimal 8482. 153 is the byte encoding in Windows code page 1252, Western European, so as a character reference &#153; shouldn't work. In practice it does in non-XML HTML dialects due to dodgy legacy browser behaviour (standardised in HTML5), but you shouldn't rely on it.

like image 53
bobince Avatar answered Oct 04 '22 03:10

bobince


The simplest answer is not to use HTML entities, but instead to use Unicode source code files with literal Unicode characters, e.g. actually put:

<%= link_to "My Trademark™", trade_mark_path %>

in your view. However, if you must, you can simply do:

<%= link_to("My Trademark&trade;", trade_mark_path).html_safe %>

to disable the automatic HTML-escaping of Rails.

Note that using decimal entities &#...; for ASCII values 128-255 is a bad idea, as these generally rely on non-standard codepages, unless you are certain that you are sending the correct encoding for your HTML that matches. As you can see, the Unicode value for the trademark symbol is actually &#8482;.

like image 45
Phrogz Avatar answered Oct 04 '22 03:10

Phrogz