0

I am using Ruby link_to in a .erb file to link to a story. The title has an ampersand in it. I want the text displayed for title to show the ampersand and not be escaped.

My data is:

story = {"title"=>"Dungeons & Dragons has its own version of the Slender Man now", "url"=>"https://www.polygon.com/22436057/dungeons-dragons-slender-man-bagman-van-richtens-guide-to-ravenloft"}

My html code has:

<h4><%= link_to story[:title], story[:url], { 'data-analytics-link' => 'readmore' } %></h4>

Here is what the link looks like with the ampersand:

Dungeons &amp; Dragons has its own version of the Slender Man now

I have tried updating the link_to to be:

<h4><%= link_to h(story[:title]), story[:url], { 'data-analytics-link' => 'readmore' } %></h4>

but I get the same result.

How can I use link_to so that the text for link shows the ampersand?

  • Is _Dungeons & Dragons_ how it shows up in the HTML source code or how it is rendered by the browser? – spickermann Jun 22 '21 at 16:13
  • Shows up in html source code as Dungeons & Dragaons ... so it is not the browser. – Roch Johnson Jun 22 '21 at 16:52
  • Then everything is fine and works as expected. `&` should be encoded to `&` in HTML because the unescaped `&` has a special meaning in HTML. Rails auto-escapes these kinds of characters automatically for you. An interesting question in this context: https://stackoverflow.com/questions/3493405/do-i-really-need-to-encode-as-amp – spickermann Jun 22 '21 at 17:21

0 Answers0