Joshua Clark
Joshua Clark

Reputation: 1356

Embed link into string and then display string (with link_to and gsub!)

Here's what I'm trying to do. It's intended to replace plain text with a clickable link

<%= r.text.gsub!("\##{ht[:text]}", link_to("\##{ht[:text]}", "www.url.com")) %>

Where r.text is a string. But when it displays, the HTML is embedded in the text, it is not a link.

Some text <a href="www.url.com">Some other text</a>

How do I make it display the link?

Upvotes: 0

Views: 1015

Answers (2)

Igor Kapkov
Igor Kapkov

Reputation: 3909

try <%= raw(r.text.gsub!("\##{ht[:text]}", link_to("\##{ht[:text]}", "www.url.com"))) %>

Upvotes: 0

Sean Hill
Sean Hill

Reputation: 15056

You need to use html_safe, i.e. some_string.html_safe. Just make sure that the rest of the content is safe to display, being stripped of any other potentially malicious html. So you for your specific case, you'd do this:

<%= r.text.gsub!("\##{ht[:text]}", link_to("\##{ht[:text]}", "www.url.com")).html_safe %>

Upvotes: 2

Related Questions