alste
alste

Reputation: 1455

Why is the string that I constructed rendered as escaped HTML?

I want to add a link to each element of an array, then join the resulting array. I tried:

myarray.collect{|u| link_to u[:first_name], username_path(u[:username])}.join(', ')

This does everything correctly, except it returns:

<a href="/niels">Niels Bohr</a>, <a href="/richard">Richard Feynman</a>

Instead of

<a href="/niels">Niels Bohr</a>, <a href="/richard">Richard Feynman</a>

How do I fix this? Or is there a simpler way of proceeding?

Thanks.

Upvotes: 0

Views: 73

Answers (2)

Confusion
Confusion

Reputation: 16841

There is nothing wrong with adding the links or joining the elements of the list. That all works fine. What is wrong is that your string is considered unsafe and some of the characters used to construct valid HTML (and more importantly, javascript) are being escaped.

As fl00r says, you should add .html_safe after the string, to tell the rendering function that any HTML in the function can be safely sent to the browser as-is.

Upvotes: 1

fl00r
fl00r

Reputation: 83680

Use html_safe

myarray.collect{|u| link_to u[:first_name], username_path(u[:username])}.join(', ').html_safe

Upvotes: 4

Related Questions